CF724D. Dense Subsequence[贪心 字典序!]
2 seconds
256 megabytes
standard input
standard output
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 ≤ i1 < i2 < ... < it ≤ |s|. The selected sequence must meet the following condition: for every j such that 1 ≤ j ≤ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, j + m - 1], i.e. there should exist a k from 1 to t, such that j ≤ ik ≤ j + m - 1.
Then we take any permutation p of the selected indices and form a new string sip1sip2... sipt.
Find the lexicographically smallest string, that can be obtained using this procedure.
The first line of the input contains a single integer m (1 ≤ m ≤ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
3
cbabc
a
2
abcab
aab
3
bcabcbaccba
aaabb
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab".
题意:至少每m个选一个字符使得选择的字符字典序最小的排列的字典序最小
和那个经典问题很想,就是多了选择的字符可以任意排列
要深入理解字典序
考虑只选a,可行就只选a,否则就要选b,此时a一定全选(字典序最小),b尽量少的选
枚举选到哪个字符就行了
PS:貌似可以二分,然而才26个字符并不需要
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e5+,INF=1e9+; int n,m,a[];
char s[N];
bool sol(char c){
int last=,cnt=,lc=;
for(int i=;i<=n;i++){
if(s[i]==c) lc=i;
if(s[i]<c) last=i;
if(i-last>=m){
if(i-lc<m) last=lc,cnt++;
else return false;
}
} for(int i=;i<=n;i++) a[s[i]]++;
for(char now='a';now<c;now++){
while(a[now]--) putchar(now);
}
while(cnt--) putchar(c);
return true;
}
int main(){
scanf("%d%s",&m,s+);
n=strlen(s+);
for(char c='a';c<='z';c++)
if(sol(c)) break;
}
CF724D. Dense Subsequence[贪心 字典序!]的更多相关文章
- 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 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)D Dense Subsequence
传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...
- Dense Subsequence
Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- [codeforces724D]Dense Subsequence
[codeforces724D]Dense Subsequence 试题描述 You are given a string s, consisting of lowercase English let ...
- Intel Code Challenge Final Round D. Dense Subsequence 二分思想
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC026E.html 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ ...
- 【29.41%】【codeforces 724D】Dense Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 2016.10.08--Intel Code Challenge Final Round--D. Dense Subsequence
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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的数组, ...
随机推荐
- HTTP文件断点续传的原理
前几天一个同事跑过来找我说,我们在广告素材视频这块想做断点续传,就是这次某个视频缓存到一半,下次不用重头开始,可以在原来停留得位置开始继续下载.以提供更好的用户体验. 同时说需要我们支持吐素材地址的业 ...
- 纪念品分组 2007年NOIP全国联赛普及组
题目描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得的纪念品价值相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价格 ...
- Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?
487down vote Differences KEY or INDEX refers to a normal non-unique index. Non-distinct values for ...
- laravel5 数据库配置(MySQL)
laravel5 数据库配置(MySQL) 首先有一个安装完成可以运行的laravel框架. 配置database.php 进入laravel根目录. 在config目录下找到database.php ...
- 7、ASP.NET MVC入门到精通——第一个ASP.NET MVC程序
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 开发流程 新建Controller 创建Action 根据Action创建View 在Action获取数据并生产ActionResult传递 ...
- jQuery用户数字评分效果
效果预览:http://hovertree.com/texiao/jquery/5.htm HTML文件代码: <!DOCTYPE html> <html xmlns="h ...
- js中cookie的添加,删除,查询总结
function addCookie(objName,objValue,objHours){//添加cookie var str = objName + "=" + escape( ...
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(三)显示地图坐标系模块
config.xml文件的配置如下: <widget left="3" bottom="3" config="widgets/Coordinat ...
- iOS 应用的生命周期
为了研究应用的生命周期,在AppDelegate的方法里面加入打印当前的函数名的方法: 如下: 1.运行程序: 输出: 2.按一下home键 3.再点击应用 4.双击Home键,向上滑动应用,杀掉应用 ...
- ae arcgis engine 关于面转线的方法和注意事项
代码很简单,如下 private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs ...