[codeforces724D]Dense Subsequence

试题描述

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 ≤ i_1 < i_2 < ... < i_t ≤ |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 ≤ i_k ≤ j + m - 1.

Then we take any permutation p of the selected indices and form a new string si_p1si_p2... si_pt.

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.

输入示例

bcabcbaccba

输出示例

aaabb

数据规模及约定

见“输入

题解

贪心贪心使劲贪。。。先暴力求一下必须要用到哪些字母,然后除掉最后一个字母,再用个堆和滑动窗口从左往右扫一遍,贪心地取刚才除掉的那个字母。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 100010
struct Node {
char ch; int pos;
Node() {}
Node(char _, int __): ch(_), pos(__) {}
bool operator < (const Node& t) const { return ch != t.ch ? ch > t.ch : pos < t.pos; }
bool operator == (const Node& t) const { return ch == t.ch && pos == t.pos; }
} ;
priority_queue <Node> Q, delQ;
char S[maxn], ans[maxn];
int cnt, n, m; bool vis[maxn];
void solve() {
for(char ch = 'a'; ch <= 'z'; ch++) {
for(int i = 1; i <= n; i++) if(S[i] == ch) vis[i] = 1, ans[++cnt] = ch;
int lstp = 0; bool ok = 1;
for(int i = 1; i <= n; i++) {
if(vis[i]) lstp = i;
if(i - lstp == m){ ok = 0; break; }
}
if(ok) {
for(int i = 1; i <= n; i++) if(S[i] == ch) vis[i] = 0, cnt--;
break;
}
}
return ;
} int main() {
m = read();
scanf("%s", S + 1); n = strlen(S + 1); solve();
int lstp = 0;
for(int i = 1; i < m; i++)
if(vis[i]) lstp = i;
else Q.push(Node(S[i], i));
for(int i = m; i <= n; i++) {
if(!vis[i]) Q.push(Node(S[i], i));
else lstp = i;
// printf("get: %d %d\n", i, lstp);
if(i - lstp == m) {
Node u = Q.top(); Q.pop();
while(!delQ.empty() && u == delQ.top()) delQ.pop(), u = Q.top(), Q.pop();
ans[++cnt] = u.ch; lstp = u.pos;
// printf("set: %d\n", lstp);
}
if(i > m && !vis[i-m]) delQ.push(Node(S[i-m], i - m));
} sort(ans + 1, ans + cnt + 1); ans[cnt+1] = 0;
printf("%s\n", ans + 1); return 0;
}

[codeforces724D]Dense Subsequence的更多相关文章

  1. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)D Dense Subsequence

    传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...

  3. Dense Subsequence

    Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. 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 ...

  5. 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 ...

  6. 【29.41%】【codeforces 724D】Dense Subsequence

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 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 ...

  8. 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的数组, ...

  9. codeforces 724

    题目链接: http://codeforces.com/contest/724 A. Checking the Calendar time limit per test 1 second memory ...

随机推荐

  1. python_13(前端—cs)

    第1章 前端三大标准:1.1 三大标准介绍 1.2 html标签一览表 第2章 结构标准 html 2.1 html结构 2.2 html常用标签 2.2.1 h1-h6 2.2.2 span 2.2 ...

  2. poj2455Secret Milking Machine(二分+最大流)

    链接 二分距离,小于当前距离的边容量+1,使最后流>=t 注意 会有重边 #include <iostream> #include<cstdio> #include< ...

  3. 浅析cookie

    基本概念:cookie是指web浏览器存储的少量数据,该数据会在每次请求一个相关的URL时自动传到服务器中. 以博客园为例,我们看看cookie有哪些属性:  1.Name:cookie的名称: 2. ...

  4. Sql Server数据库对象访问权限控制

    以下内容主要针对database层面的数据访问权限(比如select, insert, update, delete, execute…) 1.直接给user权限GRANT EXECUTE TO [u ...

  5. 在Oracle用SQL处理以 System.currentTimeMillis

    有時為了系統的需求會紀錄到毫秒(Millisecond),我們會接將得到的值寫入db,但是如果要用SQL 做時間範圍的搜尋,有以下做法( systemdate欄位存放System.currentTim ...

  6. uva12265 Selling Land

    见紫书.(c,h)的更新策略://前面的高度为0了,直接插入因为ans==-c+h,c大,h还小,那么肯定不是最优左上角,更新新加入列的列//新的一列高度最小,就删掉了其他的,只留这个高度从上到下,从 ...

  7. ref版的 摄像头 读取 因为id的时候,id不能重复 还要用时间戳,比较麻烦

    <!-- * @description 摄像头vue版实例 * @fileName cameraObject.vue * @author 彭成刚 * @date // :: * @version ...

  8. softmax_regression完成mnist手写体数据集的识别

    ---恢复内容开始--- import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnis ...

  9. 云服务器linux使用之开发环境搭建(一)

    Host key verification failed. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: ...

  10. Hibernate中tx.commit()

    hibernate.cfg,xml文件中的自动提交事务是false.主键生成策略是native. 在表的映射继承是手动提交事务(即:tx.commit())无法发出sql语句,把数据插入到数据库的表中 ...