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 ...
随机推荐
- SpringMVC文件上传注意事项
简介 我在使用 idea 上传文件时遇到一些问题,费了好些时间,最后还是的队友来帮忙. 问题出现在没有熟练使用 IDE,采用 SpringMVC 上传文件主要注意几个事项. 1. 配置 bean 在 ...
- [IOS 开发] 自定义(重写) UITableViewCell的高亮背景色
IOS的sdk中,对UITableViewCell的高亮背景色只支持两种颜色,分别为UITableViewCellSelectionStyleBlue和UITableViewCellSelection ...
- HashTree(哈希树) ——和trie类似,只是将字符换成了质数,sphinx用到了???
摘自:http://blog.csdn.net/yang_yulei/article/details/46337405 哈希树的理论基础 [质数分辨定理] 简单地说就是:n个不同的质数可以" ...
- jsessionid 导致重定向404的问题
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html#encod ...
- 夕甲甲——孔乙己之C++版
欧欧匹代码的格局,是和别的编程模式不同的:首先要有一个构造函数:基类里只定义了函数的形式,可以随时通过派生增加不同的实现.那些程序员们,每每学会了继承和多态,便可以接一个项目,——这是十年前的事,现在 ...
- Shell父进程获取子进程的变量值
#!/bin/kshshit=""export shitshit=$(su - grid -c 'echo $ORACLE_BASE')pvar=$(subvar="he ...
- Docker简明教程(转)
Docker自从诞生以来就一直备受追捧,学习Docker是一件很炫酷.很有意思的事情.我希望通过这篇文章能够让大家快速地入门Docker,并有一些学习成果来激发自己的学习兴趣.我也只是一个在Docke ...
- 100. Same Tree
[题目] Given two binary trees, write a function to check if they are equal or not. Two binary trees ar ...
- jquery tab 插件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- -canOpenURL: failed for URL: "" - error: "(null)" , iOS9 App传输安全 支持http 解决方案
-canOpenURL: failed for URL: "CamCardHDOpenAPI:*" - error: "(null)" This app is ...