[codeforces724D]Dense Subsequence
[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的更多相关文章
- 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的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...
- Dense Subsequence
Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 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 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 ...
随机推荐
- Java迭代器的用法【转】
迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...
- rhel7使用centos7yum组件
1)rpm -qa|grep yum --查看已安装的yum组件包 2)rpm -e 包名 --nodeps --卸载包 3)下载安装以下组件包: 使用rpm -ivh yum-* yum-3.4.3 ...
- phpstorm设置代码块快捷方式
File -> Settings -> Live Templates
- 工作中Git使用笔记
git相关说明. //git 安装$ git config --global user.name "xxx"代码提交时的用户名,与GITLAB注册用户名建议保持一致$ git co ...
- 使用原生JavaScript模拟getElementByClassName .
最近在工作中,由于有一个插件必须使用jquery-pack.js,而这个包又是非常古老的jquery,所以又的函数是无法使用的,例如$()选择器以及parent()都取不到标签的内容. 所以没办法,只 ...
- os x 中出现message sent to deallocated instance 的错误总结
一般是程序中的某一个对象被release 了两次 一般情况下是与你定义的类型有关 这里面我的错误是吧 NSString 类型的变量的属性 设置为了 assign 了 目测与这个有关 补充object- ...
- uva12265 Selling Land
见紫书.(c,h)的更新策略://前面的高度为0了,直接插入因为ans==-c+h,c大,h还小,那么肯定不是最优左上角,更新新加入列的列//新的一列高度最小,就删掉了其他的,只留这个高度从上到下,从 ...
- Deep_into_iris
具体ipynb文件请移步Github #各种所需要的库函数首先加载 import numpy as np import pandas as pd import matplotlib.pyplot as ...
- Selenium3+python自动化008-操作浏览器基本方法
一.打开网站1.第一步:从selenium里面导入webdriver模块2.打开Firefox浏览器(Ie和Chrome对应下面的)3.打开百度网址二.页面刷新1.有时候页面操作后,数据可能没及时同步 ...
- luogu P1519 穿越栅栏 Overfencing
题目描述 描述 农夫John在外面的田野上搭建了一个巨大的用栅栏围成的迷宫.幸运的是,他在迷宫的边界上留出了两段栅栏作为迷宫的出口.更幸运的是,他所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意 ...