Codeforces Gym 100114 D. Selection
Description
问选择一个序列上的所有数的最少操作次数,跟电脑上选择文件一样,输出操作方案.
Sol
贪心.
用Shift一段文件只能使用一次,之后必须一直按Ctrl了.
然后就是看用Shift选哪段区间了.
如果选 \((l,r)\) 那么答案就是 \((C_{l-1})+(C_{n}-C_{r})+(r-l+1-C_{r}+C_{l-1})+2\) .
化简就是 \(C_{n}+r-2C_{r}-(l-1)+2C_{l-1}+2\)
这个决策点是唯一的,只需要计算答案的时候顺便统计一下 \((l-1)+2C_{l-1}\) 就可以了.
否则就是用Ctrl全选上.
复杂度 \(O(n)\)
Code
#include<cstdio>
#include<iostream>
using namespace std; const int N = 100005; int n;
char c[N];
int s[N]; int ans[3]; int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout); scanf("%d",&n);scanf("%s",c+1);
for(int i=1;i<=n;i++) s[i]=s[i-1]+(c[i] == '*'); // for(int i=1;i<=n;i++) cout<<s[i]<<" ";cout<<endl; int tmp=0,u;ans[0]=n+2;
for(int i=1;i<=n;i++){
u=s[n]-2*s[i]+i+2;
if(2*s[tmp]-tmp+u < ans[0]){
ans[1]=tmp+1,ans[2]=i;
ans[0]=2*s[tmp]-tmp+u;
}
if(2*s[tmp]-tmp > 2*s[i]-i) tmp=i;
} if(ans[0]<s[n]){
printf("%d\n",ans[0]);
printf("%d\n",ans[1]);
printf("Shift+%d\n",ans[2]);
for(int i=1;i<ans[1];i++) if(s[i]-s[i-1]) printf("Ctrl+%d\n",i);
for(int i=ans[1];i<=ans[2];i++) if(s[i]-s[i-1]==0) printf("Ctrl+%d\n",i);
for(int i=ans[2]+1;i<=n;i++) if(s[i]-s[i-1]) printf("Ctrl+%d\n",i);
}else{
printf("%d\n",s[n]);
int f=0;
for(int i=1;i<=n;i++){
if(s[i]-s[i-1]){
if(!f) printf("%d\n",i),f=1;
else printf("Ctrl+%d\n",i);
}
}
} return 0;
}
Codeforces Gym 100114 D. Selection的更多相关文章
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- Codeforces GYM 100114 C. Sequence 打表
C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...
- Codeforces GYM 100114 B. Island 水题
B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- Codeforces Gym 100114 J. Computer Network
Description 给出一个图,求添加一条边使得添加后的图的桥(割边)最少. Sol Tarjan. 一遍Tarjan求割边. 我们发现连接的两个点一定是这两个点之间的路径上的桥最多,然后就可以贪 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
随机推荐
- rem是如何实现自适应布局的?
http://caibaojian.com/web-app-rem.html 使用rem 然后根据媒体查询实现自适应.跟使用JS来自适应适配也是同个道理,不过是js更精确一点.使用媒体查询: html ...
- Hadoop-2.7.3 问题收集
问题1:在DFS Lcation 上不能多文件进行操作: 在Hadoop上的每个节点上修改该文件 conf/mapred-site.xml 增加: <property> < ...
- Codeforces Problem 708A Letters Cyclic Shift
题目链接: http://codeforces.com/problemset/problem/708/A 题目大意: 从字符串s中挑选出一个子串(非空),将该子串中的每个字母均替换成前一个字母,如' ...
- Which hashing algorithm is best for uniqueness and speed?
http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness- ...
- Jsp与servlet的区别 1
Jsp与servlet的区别 2011-12-09 16:27:47 分类: Java 1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识 ...
- lombok+slf4j+logback SLF4J和Logback日志框架详解
maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...
- [webgrid] – Ajax – (Reloading a Razor WebGrid after Ajax calls using a partial view)
Reloading a Razor WebGrid after Ajax calls using a partial view If you are using Razor and MVC you p ...
- iOS 在UILabel显示不同的字体和颜色(转)
转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...
- 浅谈JavaScript中的事件
引言 Html页面与JavaScript之间的交互是通过事件来完成的.事件,就是文档或者浏览器窗口中发生的一些特定的交互瞬间.可以使用侦听器(处理程序)来预订事件,以便事件发生时执行相应的代码.这在传 ...
- 使用/调用 函数的时候, 前面加不加 对象或 this?
这个问题, 其实没有细想: 应该是这样的: (想明白了, 就会少很多困惑, 会对语言的把握 会 更深入更透彻) 任何一门 语言, (如果你自己去设计一门语言...). 都要规定 一些 "关键 ...