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个取一个最小的(坐标尽量靠后);
这样选完后扫一遍字符串,那些没有被选取且不是当前答案里最大的字符也能加入答案;
最后排序即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,vis[maxn];
struct node
{
char x;
int pos;
node(char _x,int _pos):x(_x),pos(_pos){}
bool operator<(const node&p)const
{
return x==p.x?pos>p.pos:x<p.x;
}
};
char a[maxn],ma;
string ans;
set<node>pq;
int main()
{
int i,j;
scanf("%d%s",&n,a);
int len=strlen(a),pre=;
for(i=;i<n;i++)pq.insert(node(a[i],i));
i=n;
while()
{
node now=*pq.begin();
ans+=now.x;
ma=max(ma,now.x);
vis[now.pos]=;
for(j=pre;j<=now.pos;j++)pq.erase(node(a[j],j));
pre=now.pos+;
int cnt=;
for(j=i;j<len&&j-now.pos<=n;j++)
{
i=j;
cnt=j-now.pos;
pq.insert(node(a[j],j));
}
i++;
if(cnt<n)break;
}
for(i=;a[i];i++)if(!vis[i]&&a[i]<ma)ans+=a[i];
sort(ans.begin(),ans.end());
cout<<ans<<endl;
//system("Pause");
return ;
}
Dense Subsequence的更多相关文章
- CF724D. Dense Subsequence[贪心 字典序!]
		
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
 - Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)D Dense Subsequence
		
传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...
 - 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 ...
 - [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 ...
 - 【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的数组, ...
 - codeforces 724
		
题目链接: http://codeforces.com/contest/724 A. Checking the Calendar time limit per test 1 second memory ...
 
随机推荐
- PHP截取中文字符串方法总结
			
<?php @header('Content-type: text/html; charset=UTF-8'); $arr = "sa撒的发dfa多少sfd看sdf得12上24飞452 ...
 - Talking Ben App砸壳记
			
需求: 导出Talking Ben app的头文件 实施: 1)准备材料: 越狱IOS设备一部,并安装Talking Ben游戏 IOS设备上安装open SSH IOS设备的/usr/bin 中安装 ...
 - openwrt杂记
			
/etc/config/wireless是在boot启动时生成的. 代码在/etc/init.d/boot中,如下: /sbin/wifi detect > /tmp/wireless.tmp ...
 - Linux(power服务器)中kettle(2)
			
Hadoop集群硬件环境 4台机器 ip地址 172.16.1.131 172.16.1.132 172.16.1.133 172.16.1.134 每台内存16G 8核cpu 直接使用报错:
 - 关于c++的引用
			
引用的本质 引用事实上就是两个变量指向同一个地址 int x; int &y = x; cout << &x << endl; cout << &a ...
 - Just do it!!!
			
从今日起,开个开发自己个人轻量级博客,加油!!!!!
 - Android Lights
			
Android Lights 很多Android手机上都配有LED灯,手机在充电.新来短信等时候都会有相应的指示灯提示. Android系统之中,一共定义了8个逻辑灯,包含:背光,键盘灯,按键灯,充电 ...
 - Github 修正上传时“this exceeds GitHub’s file size limit of 100 MB”错误
			
自己的项目的版本控制用的是Git,代码仓库在github托管.项目里用到了IJKMediaFramework 想把代码push到github上,结果出错了,被拒绝,具体信息是: Total 324 ( ...
 - XueTr 0.45 (手工杀毒辅助工具)  绿色版
			
软件名称: XueTr 0.45 (手工杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 3.3MB图片预览: ...
 - 高性能MySQL第2,3章性能相关 回顾笔记
			
1. 基准测试(benchmark) 不管是新手还是专家都要熟悉基准测试,benchmark测试是对系统的一种压力测试,目标是为了掌握在特定压力下系统的行为.也有其他原因:如重现系统状态,或者是 ...