贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D
http://codeforces.com/contest/724/problem/D
题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中最小的。
从pos=0开始挑选,每次挑选pos~pos+m-1这些位置中的字符,然后下一次再从之前挑选字符的位置开始的后面的m个中进行挑选。
思路:先贪心,按照字典序,每次找到该范围区间内最小的字符,因为该字符一定是串中的一部分,所以必然会被挑选的,于是我们先把这个pos的字符放入集合,并且对挑选过的位置做上标记。按照之前的方法可以先得到了一个集合,但是该集合不一定是最小的,例如m=2的时候cbbadca你前面挑选的集合为baca,但是你会发现还有一个b,如果放入的话字典序会更小,所以我们先得到目前集合中最大的字符,令他为ch,再for一遍把比ch小的再给放进来就好了。
说一下树状数组的作用,因为统计到末尾的时候,我不知道是直接结束还是再放入末尾区间内的某个字母,所以我利用树状数组统计了一下前面所有字符出现的次数,然后查询区间比末尾区间最小的字符串大的字符是否存在,如果存在就放入。
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int n, chlen;
char ch[maxn];
int tree[ + ];
vector<char> v;
char s[maxn];
bool vis[maxn]; void update(int x, int val = ){
for (int i = x; i <= ; i += i & -i){
tree[i] += val;
}
} int sum(int x){
int ans = ;
for (int i = x; i > ; i -= i & -i){
ans += tree[i];
}
return ans;
}
///①并不是越长越好 ②最短长度是已经确定了的③所选出来的一定要比最短的那个的字典序要小
///目标,长度短,字典序短
void solve(){
int pos = , slen = strlen(s);
char minich = ch[pos];
for (int i = pos + ; i < n; i++){
if(minich >= ch[i]){minich = ch[i], pos = i;}
}
vis[pos] = true;
v.pb(minich);
while (pos < chlen){
int minipos = pos + ; minich = ch[minipos];
for (int i = minipos + ; i <= min(chlen, pos + n); i++){
if (minich >= ch[i]){minich = ch[i], minipos = i;}
}
if (pos + n > chlen){
int val = sum() - sum(minich);
if (val == ) break;
}
vis[minipos] = true;
v.pb(minich);
update(minich);
pos = minipos;
}
sort(ALL(v));
char maxch = v[v.size() - ];
for (int i = ; i <= chlen; i++){
if (!vis[i] && maxch > ch[i]){
vis[i] = true; v.pb(ch[i]);
}
}
} int main(){
while (scanf("%d", &n) == ){
memset(vis, false, sizeof(vis));
memset(tree, , sizeof(tree));
memset(s, , sizeof(s));
memset(ch, , sizeof(ch));
scanf("%s", ch);
chlen = strlen(ch);
if (n > chlen) {printf("\n"); continue;}
chlen--;
int cnt = ;
for (int i = ; i <= chlen; i += ){
s[cnt++] = ch[i];
}
solve();
sort(ALL(v));
for (int i = ; i < v.size(); i++){
printf("%c", v[i]);
}
printf("\n");
v.clear();
}
return ;
}
贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D的更多相关文章
- CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力枚举,水 1.题意:n*m的数组, ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)
题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...
- 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】
呵呵哒,上分~ CodeForces 724A: 题意: 给你两个星期几,问连续两个月的头一天是否满足: #include <iostream> #include <stdio.h& ...
- 模拟。。。 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C
题目大意:给你一个n*m的矩阵,再给你一个小球,从(0,0)以sqrt(2)/s的速度向右上角出发,遇到边框会反弹,遇到角落就直接停止,给你一些点,问小球第一次经过这些点所需要的时间. 思路:模拟一下 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划
E. Goods transportation 题目连接: http://codeforces.com/contest/724/problem/E Description There are n ci ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 暴力
D. Dense Subsequence 题目连接: http://codeforces.com/contest/724/problem/D Description You are given a s ...
- 最小割dp Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E
http://codeforces.com/contest/724/problem/E 题目大意:有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i<j,可以 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)D Dense Subsequence
传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...
随机推荐
- vs2008试用版的评估期已经结束解决办法
1.控制面板----程序和功能----找到VS2008,打开"卸载/更改". 2.下载补丁:files.cnblogs.com/elaky/PatchVS2008.zip 打补丁之 ...
- 邮件发送 java
package com.sun.mail; import java.io.File;import java.io.IOException;import java.io.UnsupportedEncod ...
- MySQL数据类型和属性
日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:2014-09-18 time 3字节,时间,格式:08:42:30 datetime 8字节,日期时间,格式:2014-0 ...
- luci-bwc(记录流量功能)
使用方法如下: root@openwrt:/# luci-bwcUsage: luci-bwc [-t timeout] -i ifname // ifname为 ...
- 浅析IO模型
也许很多朋友在学习NIO的时候都会感觉有点吃力,对里面的很多概念都感觉不是那么明朗.在进入Java NIO编程之前,我们今天先来讨论一些比较基础的知识:I/O模型.下面本文先从同步和异步的概念 说起, ...
- nefu 903 字符串去星
字符串去星 Problem : 903 Time Limit : 1000ms Memory Limit : 65536K description 有一个字符串(长度小于100),要统计其中有多少个* ...
- sqlite 学习
到谷歌上搜sqlite,第一项便是官方网站:www.sqlite.org.进去后,先了解一下大体,感觉还不错. 进入Document页面,大标题SQLite Programming Interface ...
- erlang四大behaviour之三-gen_event
来源:http://www.cnblogs.com/puputu/articles/1689623.html 1. 事件处理规则 在OTP中,事件管理器是一个事件可以发送到的命名对象,一个事件可以是一 ...
- 【Machine Learning in Action --5】逻辑回归(LogisticRegression)从疝气病预测病马的死亡率
背景:使用Logistic回归来预测患有疝气病的马的存活问题,这里的数据包括368个样本和28个特征,疝气病是描述马胃肠痛的术语,然而,这种病并不一定源自马的胃肠问题,其他问题也可能引发疝气病,该数据 ...
- alertview 添加图片
- (void)willPresentAlertView:(UIAlertView *)alertView { 在这个方法中, 绘制需要的东西 uiview *myView = [uiview all ...