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 ...
随机推荐
- 20169212《Linux内核原理与分析》第六周作业
视频学习 一.用户态.内核态和中断 内核态:处于高的执行级别下,代码可以执行特权指令,访问任意的物理地址,这时的CPU就对应内核态 用户态:处于低的执行级别下,代码只能在级别允许的特定范围内活动.在日 ...
- ubuntu16.04 orbslam ./build.sh 出错eigen
错误如下: /home/a/ORB_SLAM2/src/Optimizer.cc:1244:1: required from here/usr/include/eigen3/Eigen/src/Cor ...
- mysql5.7碰到的坑
日志输出时区问题 输出日志有这些信息2016-08-16T02:23:09.831827Z 112241 [Note] Aborted connection 112241 to db: 'test' ...
- 自定义BadgeView
-(instancetype)initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { self.u ...
- Fake chat script for website download
Are you searching for free fake webchat script then you are at the right place go get download your ...
- (转)redis 3.0的集群部署
一:关于redis cluster 1:redis cluster的现状 reids-cluster计划在redis3.0中推出,可以看作者antirez的声明:http://antirez.com/ ...
- 【Linux】【通信】1.ping不通
关于为什么ping不通有很多种原因,但直接的表象就网络之间没有成功进行通讯: 在构建虚拟机和win之间的交互时,主要使用了3种网络模式: 桥接bridge VMnet0 主机host VMne ...
- curl post
//Post方式实现 $url = "http://localhost/web_services.php"; $post_data = array ("username& ...
- 【墙内备份】Android 6.0 APIs
Android 6.0 APIs In this documentSHOW MORE Fingerprint Authentication Confirm Credential App Linking ...
- ASP.NET4.5从零开始(1)
使用Web窗体 引言 前段时间客户A突然提出,B项目希望可以做成BS形式的,之前是CS的,因为我之前也没学过ASP.NET,于是一边百度,一边Coding,马马虎虎的把功能流程给调通,然后就交差了,想 ...