贪心+树状数组维护一下 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的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...
随机推荐
- Linux使用rsync客户端与服务端同步目录进行备份
一.服务端设置 1. 修改 server 端配置 # vi /etc/rsyncd.conf 修改: uid = nobody # 该选项指定当该模块传输文件时守护进程应该具有的uid.默认值为&qu ...
- 《JS权威指南学习总结--8.8 函数式编程和8.8.1使用函数处理数组》
内容要点: 和Lisp.Haskell不同,JS并非函数式编程语言,但在JS中可以像操控对象一样操控函数, 也就是说可以在JS中应用函数式编程技术.ES5中的数组方法(诸如map()和red ...
- 【Sort】RadixSort基数排序
太晚了,明天有时间在写算法思路,先贴代码 ------------------------------------------------ 刚答辩完,毕业好难,感觉自己好水 ------------- ...
- Django中使用Bootstrap
一.在Django中引用Bootstrap模版 1.首先下载bootsrtap代码(http://v3.bootcss.com/getting-started/#download),并将下载后的文件放 ...
- luci编译错误
make[3]: Entering directory '/home/hbg/test1214/package/feeds/luci/luci'*** Repository layout change ...
- 官方解答:Vultr VPS常见问题
VULTR VPS配置高,价格低廉,是非常优秀的vps品牌.今天我来翻译vultr官方FAQ,相信你能找到具体答案. Q 请介绍VULTR VPS机器硬件配置 Intel CPU 3+ GHz Cor ...
- js转义
$('select[name="conditions[\'examQuestion.examTypeId_int\'].value"]');JS中使用 \' 作为 ' 转义
- NOIP2011-普及组复赛模拟试题-第一题-NBA总冠军
题目背景 Background 一年两度的期末考要到来了!! 题目描述 Description 又要到考试了,Ljw决定放松一下,就打开电视,看见了篮球赛,他立即想到了每年的NBA总冠军队伍.由 ...
- SSL+socket详解
转自:http://hengstart.iteye.com/blog/842529 一. SSL概述 SSL协议采用数字证书及数字签名进行双端实体认证,用非对称加密算法进行密钥协商,用对 ...
- MySQL5.5.源码安装
MySQL5.5.34安装需要用到cmke ncurses-devel yum install -y ncurses-devel cmake gcc gcc-c++ bison 下载http://m ...