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 ...
随机推荐
- 你应该知道的CSS文字大小单位PX、EM、PT
这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章, 题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用 ...
- JS的异步回调函数
hi :)几日不见,趁着周末和父母在广州走走逛逛,游山玩水,放松身心,第一天上班就被一个问题难住了,不废话,以下是关于JS函数回调方面的知识,今天的查阅看的也是一知半解,摘录下来日后慢慢琢磨! js中 ...
- Linux启动新进程的几种方法及比较[转]
有时候,我们需要在自己的程序(进程)中启动另一个程序(进程)来帮助我们完成一些工作,那么我们需要怎么才能在自己的进程中启动其他的进程呢?在Linux中提供了不少的方法来实现这一点,下面就来介绍一个这些 ...
- 设置 SSH 免密码登陆——仍提示输入密码
1)生成密钥:在根目录下(cd ~/ 用户根目录)执行如下语句: ssh-keygen -t dsa -P ' ' -f ~/.ssh/id_dsa 以上是两个单引号. 2)将id_dsa.pu ...
- 城市字符串----转数组( 加空格---preg_split) 正则分割字符串 --> 成数组
正则 以 斜杠开始 斜杠 结束 斜杠 包围起来 / / 字母 反斜杠 转义 \s space 匹配空格 多个空格 [ \s ]+ 中括号 包围起来 public function ...
- Qt5 Cmake
project(my) cmake_minimum_required(VERSION ) set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.3.0\\5.3\\msvc ...
- ElasticSearch基础(4)-索引
一.ES API常用规则 ES支持以Http协议的方式提供REST服务,以JSON格式发送请求返回响应. ES提供了大量的不管的数据操作,运维管理API,大量的api 这海量的api有一些通用的功能特 ...
- Java "==" 和 "equals" 和 "" 问题
//equals()方法出现的问题 String a="testd"; String b="testd"; String c=new String(" ...
- 关于 JAVA 中使用 Preferences 读写注册表时要注意的地方
要注意的只有一个地方,那就是键名或者项名不要包含大写字母,否则读不到数据. 代码是这样的: 1: Preferences preferences = Preferences.systemRoot(); ...
- MC- 交易并设置止损
using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; using ATCenterP ...