Intel Code Challenge Final Round D. 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".
题意:给你一个长度至多1e5的字符串,和一个数字m(<=1e5),要求你找出一组下标,使得任意的长度为m的连续的字符串序列都包括你选的下标,输出字典序最小的一组解;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
#define SC scanf
typedef long long ll;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int N=1e5+;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;}; char s[N];
int num[];
int main()
{
int m;
while(~SC("%d",&m)){
MM(num,);
SC("%s",s+);
char c;
for(c='a';c<='z';c++){
int k=,flag=;
for(int i=;s[i]!='\0';i++){
k++;
if(s[i]<=c) {
k=;
if(s[i]==c){
num[c-'a']++;
}
}
if(k>=m) flag=;
}
if(flag) break;
}
num[c-'a']=; int k=,now;
for(int i=;s[i]!='\0';i++){
k++;
if(s[i]<c) k=;
else if(s[i]==c) now=i;
if(k==m) {
num[c-'a']++;
i=now;
k=;
}
}
for(char x='a';x<=c;x++)
{
while(num[x-'a']--) {
printf("%c",x);
}
}
printf("\n");
}
return ;
}
分析:
1.首先可以发现从从a到z枚举出选出的字符的最大值的话,那么要保证字典序最小的话,比当前枚举的字符小的都要选取(二分思想)
2.那么我们只要从a到z枚举选取的字符的最大值qq,首先所有qq都选,判断能否满足题意;
3.接下来只要确定qq的个数就好了,从左到右枚举,一当出现k==m的情况,就选取最近的一个qq
Intel Code Challenge Final Round D. Dense Subsequence 二分思想的更多相关文章
- 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的数组, ...
- 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 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort
链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing
我不告诉你这个链接是什么 分析:模拟可以过,但是好烦啊..不会写.还有一个扩展欧几里得的方法,见下: 假设光线没有反射,而是对应的感应器镜面对称了一下的话 左下角红色的地方是原始的的方格,剩下的三个格 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)
题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)
传送门 Description You are given a table consisting of n rows and m columns. Numbers in each row form a ...
随机推荐
- MySQL - 性能优化 & MySQL常见SQL错误用法(转载)
1. LIMIT 语句 分页查询是最常用的场景之一,但也通常也是最容易出问题的地方.比如: , ; 一般DBA想到的办法是在type, name, create_time字段上加组合索引.这样条件排序 ...
- Kirinriki 2017多校
由于每个串的长度为5000,我们去枚举两个自串的对称点(这里注意一下,枚举的时候有两种情况的区间),然后用尺取法爬一遍. ac代码: #include<iostream> #include ...
- hdu 2610 2611 dfs的判重技巧
对于全排列枚举的数列的判重技巧 1:如果查找的是第一个元素 那么 从0开始到当前的位置看有没有出现过这个元素 出现过就pass 2: 如果查找的不是第一个元素 那么 从查找的子序列当前位置的前一个元素 ...
- [javascript]localStorage和sessionStorage区别
一.sessionStorage.localStorage.cookie可查看的位置,F12=>Application: 二.cookie .sessionStorage与localStorag ...
- WindowsAPI使用详解——GetVersion|GetVersionEx 获取操作系统版本和名称
Windows API 中有两个函数可以得到系统版本信息:GetVersion和GetVersionEx. GetVersion这个函数曾经困扰了很多程序员,其本来设计的是在DWORD返 ...
- js中数组方法及分类
数组的方法有很多,这里简单整理下常用的21种方法,并且根据它们的作用分了类,便于记忆和理解. 根据是否改变原数组,可以分为两大类,两大类又根据不同功能分为几个小类 一.操作使原数组改变 1.数组的 ...
- ThreeJS中创建文字的几种方法
1. DOM + CSS 传统html5的文字实现,用于添加描述性叠加文字的方法.一般使用绝对定位,并且保证z-index够大,用于显示在3D场景之上. 优点: 与CSS3D效果一致 缺点: 3d效果 ...
- php实现多进程、多线程
孤儿进程:一个父进程退出,而它的一个或多个子进程还在运行,那么那些子进程将成为孤儿进程.孤儿进程将被init进程(进程号为1)所收养,并由init进程对它们完成状态收集工作. 僵尸进程:一个进程使用f ...
- CentOS自动备份MySql
1.确认Crontab是否安装 service crond startcrontab -l 2.编写备份脚本 cd mkdir backup cd backup vim auto.sh /usr/bi ...
- host文件简介及修改后不能保存解决方法
一.文件概述 Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先 ...