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 ...
随机推荐
- ionic 初入门
ionic ionic 是webapp开发的一个框架 安装 npm install -g cordova ionic ; 我这两个分开装,因为ionic模块拖不下来,所以只好等待时机.这时候科学上网 ...
- php案列分享
<?php function GetfourStr($len) { $chars_array = array( "0", "1", "2&quo ...
- django服务器配置
服务器配置是Ubuntu14.04 64位OS ubuntu14.04默认是安装好了python2.7版本不用自己安装了. 先更新下源 sudo apt-get update 第一步先安装pip su ...
- MVC3 新建项目
一.安装工具 二.新建项目 step1:新建MVC3项目 打开新建项目窗口,在“已安装的模板”列表中选择“Web”,在右侧应用程序模板列表中选择“ASP.NET MVC3 Web应用程序”,修改项目名 ...
- Maven学习链接
别人的资料很多且写的很详细,我这里先收藏,等学习到一定阶段且有时间再整理自己的积累. 1.eclipse安装maven插件方法: http://blog.csdn.net/kittyboy0001/a ...
- 使用Autolayout对多行文本Label进行布局,高度不准确的解决办法!
BUG描述: 今天公司的项目中发现了一个BUG,大概给大家描述一下,tabbleView有一个tableFooterView,这个footView中有一个Label,是多行显示文本,程序用的是Auto ...
- mysql取前几行数据limit用法
转自http://www.cnblogs.com/study100/archive/2013/07/30/3224250.html 在mysql中是没有top关键字的,在mysql中可以用limit来 ...
- sphinx索引分析——文件格式和字典是double array trie 检索树,索引存储 – 多路归并排序,文档id压缩 – Variable Byte Coding
1 概述 这是基于开源的sphinx全文检索引擎的架构代码分析,本篇主要描述index索引服务的分析.当前分析的版本 sphinx-2.0.4 2 index 功能 3 文件表 4 索引文件结构 4. ...
- WPF附加属性
附加属性实质也是依赖属性,是说一个属性本来不属于某个对象,但由于某种需求被后来附加上的,也就是说把对象放入一个特定环境后才具有的属性 例子:人在学校有年纪和班级两个属性,人放在学校里会获得年级和班级两 ...
- oracle 函数写法 总结
1:首先看创建一个函数 给定一个日期,判断是否是休息日. create or replace function test(date_in in date) return int is num int; ...