codeforces 724D(贪心)
题目链接:http://codeforces.com/contest/724/problem/D
题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有s的位置(不是字符),求所有s在sort后字典序最小的那个字符串。
思路:对字符排序后,从最后一个开始贪心,判断删除该字符后是否符和题意,当删除后不符合题意时,贪心到该相同字符对应的第一个位置为止。
比如对于test3来说
排序后为a a a b b b b c c c c
删除到(b,6)时发现不合题意了,该相同字符对应的第一个位置为(b,3),继续贪心到3为止,结果为a a a b(第二个b) b(第四个b)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + ;
char a[N];
struct node
{
char c;
int id;
node() {}
node(char c,int i) : c(c), id(i) {}
bool operator < (const node &t)
{
if(c != t.c)
return c < t.c;
return id < t.id;
}
}q[N];
map<int,bool> mp;
map<int,bool>::iterator it;
int main()
{
int m;
scanf("%d",&m);
scanf("%s",a);
int len = strlen(a);
for(int i = ; i < len; i++)
{
q[i].c = a[i];
q[i].id = i;
mp[i]++;
}
mp[-] = ,mp[len] = ;
sort(q,q + len);
for(int i = len - ; i >= ; i--)
{
it = mp.find(q[i].id);
it--;
int t1 = (*it).first;
it++,it++;
int t2 = (*it).first;
if(t2 - t1 > m)
{
int t = lower_bound(q, q + i + , node(q[i].c,)) - q;
for(int j = ; j < t; j++)
printf("%c",q[j].c);
for(int j = i; j >= t; j--)
{
it = mp.find(q[j].id);
it--;
int t3 = (*it).first;
it++,it++;
int t4 = (*it).first;
if(t4 - t3 <= m)
{
it--;
mp.erase(it);
continue;
}
printf("%c",q[j].c);
}
return ;
}
it--;
mp.erase(it);
}
return ;
}
codeforces 724D(贪心)的更多相关文章
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
- Codeforces 732e [贪心][stl乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...
- Codeforces 721D [贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一列数a,可以进行k次操作,每次操作可以选取任意一个数加x或者减x,x是固定的数.求如何才能使得这个数列所有数乘积最小. 思路: 贪心...讨 ...
- CodeForces - 424B (贪心算法)
Megacity Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
随机推荐
- Oracle管理口令遗忘解决办法
刚装Oracle11g后忘记了安装时设置的管理口令,可以通过如下方法重置指定用户名密码: 1.打开sql plus,在“请输入用户名”提示符后输入“/as sysdba”,链接数据库后通过“alter ...
- Python【7】-数据分析准备
一.经常用到的python库: Numpy:Python科学计算的基础包: pandas:提供了能使我们快捷的处理结构化数据的大量数据结构和函数: matplotlib:用于绘制数据图表的python ...
- checkbox的check事件
<input type="checkbox" id="NameFlag" name="originalName" onClick=&q ...
- EF6 CodeFirst 实践系列文章列表
2015 Jul.16 EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(一) 来自:wangweimutou 本系列源自对EF6 CodeFirst的探索 ...
- Adapter 启动时报错——2
在安装tibco adr3 7.00以前的版本,在designer中启动adr3 会报出“无法加载adr3.dll”文件的错误,这是因为在designer中的palettes默认是指向adapter ...
- android 打包失败
如果在打包时提示: proguard returned with error code 1 并且在控制台中输出:'java'不是内部或外部程序或批处理文件 那么就说明你的环境变量出问题了,之前用的 ...
- android开发-小技巧篇(集合)
1.对于过多的控件,功能类似,数量又多的,可以用include方法.在实现应用中,可以把控件放入List集合中. private void initView() { // TODO Auto-gene ...
- Themida和Winlicense加壳软件脱壳教程
(一)Themida和不用license的Winlicense加壳软件就不说了,直接上脚本脱壳. (二)先看看不同版本OEP的一些小特征: Temida2.1.X.X版本之后的OEP特征(2.0.8. ...
- hihoCoder#1014
刚开始学习C语言,准备在做hiho的题目的过程中来学习,在此进行记录,如果代码中有错误或者不当的地方还请指正. 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小 ...
- EF快速开发定义数据接口类(转)
using System; using System.Linq; using System.Linq.Expressions; using System.Data.Objects; namespace ...