贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D
http://codeforces.com/contest/724/problem/D
题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中最小的。
从pos=0开始挑选,每次挑选pos~pos+m-1这些位置中的字符,然后下一次再从之前挑选字符的位置开始的后面的m个中进行挑选。
思路:先贪心,按照字典序,每次找到该范围区间内最小的字符,因为该字符一定是串中的一部分,所以必然会被挑选的,于是我们先把这个pos的字符放入集合,并且对挑选过的位置做上标记。按照之前的方法可以先得到了一个集合,但是该集合不一定是最小的,例如m=2的时候cbbadca你前面挑选的集合为baca,但是你会发现还有一个b,如果放入的话字典序会更小,所以我们先得到目前集合中最大的字符,令他为ch,再for一遍把比ch小的再给放进来就好了。
说一下树状数组的作用,因为统计到末尾的时候,我不知道是直接结束还是再放入末尾区间内的某个字母,所以我利用树状数组统计了一下前面所有字符出现的次数,然后查询区间比末尾区间最小的字符串大的字符是否存在,如果存在就放入。
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int n, chlen;
char ch[maxn];
int tree[ + ];
vector<char> v;
char s[maxn];
bool vis[maxn]; void update(int x, int val = ){
for (int i = x; i <= ; i += i & -i){
tree[i] += val;
}
} int sum(int x){
int ans = ;
for (int i = x; i > ; i -= i & -i){
ans += tree[i];
}
return ans;
}
///①并不是越长越好 ②最短长度是已经确定了的③所选出来的一定要比最短的那个的字典序要小
///目标,长度短,字典序短
void solve(){
int pos = , slen = strlen(s);
char minich = ch[pos];
for (int i = pos + ; i < n; i++){
if(minich >= ch[i]){minich = ch[i], pos = i;}
}
vis[pos] = true;
v.pb(minich);
while (pos < chlen){
int minipos = pos + ; minich = ch[minipos];
for (int i = minipos + ; i <= min(chlen, pos + n); i++){
if (minich >= ch[i]){minich = ch[i], minipos = i;}
}
if (pos + n > chlen){
int val = sum() - sum(minich);
if (val == ) break;
}
vis[minipos] = true;
v.pb(minich);
update(minich);
pos = minipos;
}
sort(ALL(v));
char maxch = v[v.size() - ];
for (int i = ; i <= chlen; i++){
if (!vis[i] && maxch > ch[i]){
vis[i] = true; v.pb(ch[i]);
}
}
} int main(){
while (scanf("%d", &n) == ){
memset(vis, false, sizeof(vis));
memset(tree, , sizeof(tree));
memset(s, , sizeof(s));
memset(ch, , sizeof(ch));
scanf("%s", ch);
chlen = strlen(ch);
if (n > chlen) {printf("\n"); continue;}
chlen--;
int cnt = ;
for (int i = ; i <= chlen; i += ){
s[cnt++] = ch[i];
}
solve();
sort(ALL(v));
for (int i = ; i < v.size(); i++){
printf("%c", v[i]);
}
printf("\n");
v.clear();
}
return ;
}
贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D的更多相关文章
- 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) E. Goods transportation (非官方贪心解法)
题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...
- 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】
呵呵哒,上分~ CodeForces 724A: 题意: 给你两个星期几,问连续两个月的头一天是否满足: #include <iostream> #include <stdio.h& ...
- 模拟。。。 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C
题目大意:给你一个n*m的矩阵,再给你一个小球,从(0,0)以sqrt(2)/s的速度向右上角出发,遇到边框会反弹,遇到角落就直接停止,给你一些点,问小球第一次经过这些点所需要的时间. 思路:模拟一下 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划
E. Goods transportation 题目连接: http://codeforces.com/contest/724/problem/E Description There are n ci ...
- 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 ...
- 最小割dp Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E
http://codeforces.com/contest/724/problem/E 题目大意:有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i<j,可以 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)D Dense Subsequence
传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...
随机推荐
- 【ORACLE】“System.Exception: System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本。”解决办法
我的电脑是win10.64位.问题如题,在网上找了很多办法都没搞好,如下: 1.给oracle目录设置“Authenticated Users”用户的“读取/写入”权限 2.IIS网站物理路径凭据添加 ...
- SSH综合练习-仓库管理系统-第二天
SSH综合练习-仓库管理系统-第二天 今天的主要内容: 货物入库 页面信息自动补全回显功能:(学习目标:练习Ajax交互) 根据货物简记码来自动查询回显已有货物(Ajax回显) 根据货物名来自动查询补 ...
- Storm常见问题处理
错误1:发布topologies到远程集群时,出现Nimbus host is not set异常.异常内容如下所示: [root@xop-dev-a bin]# ./storm jar /home/ ...
- vultr vps发布多用户管理功能
中国用户购买海外vps,有一个麻烦之处是付款环节,可能你没有visa信用卡,vultr和digitalocean和linode这类vps不支持支付宝,给一些朋友带来不便.由此产生的vps代购行业,其实 ...
- JAVA中计算两个时间相差多少 天,时,分,秒
1: import java.util.Date; 2: 3: public class ShowTimeInterval{ 4: public void ShowTimeInterval(Date ...
- Colorful(Folders星语多彩文件夹) v1.7绿色版
软件名称:星语多彩文件夹ColorfulFolders v1.7绿色版软件类别:国产软件运行环境:WinXP/2003/Vista/Win7/Win2008软件语言:简体中文授权方式:免费版软件大小: ...
- 杭电三部曲一、基本算法;19题 Cow Bowling
Problem Description The cows don't use actual bowling balls when they go bowling. They each take a n ...
- ORA-39070
背景介绍: 项目以前建立的库,表空间文件文件比较多,问了一下dba,了解到表空间建立很多没有带来优惠(都在一块磁盘上),效率也不会提高:现在要进行新库的迁移(目前使用的是asm磁盘组rac集群),正好 ...
- placeholder改变颜色
::-webkit-input-placeholder { /* WebKit browsers */ color: #cfcfcf; } :-moz-placeholder { /* Mozilla ...
- python模拟登录知乎
# -*- coding:utf-8 -*- import urllib import urllib2 import cookielib import sys from bs4 import Beau ...