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的更多相关文章

  1. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  2. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  3. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  4. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  5. Codeforces GYM 100114 C. Sequence 打表

    C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...

  6. Codeforces GYM 100114 B. Island 水题

    B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...

  7. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  8. Codeforces Gym 100114 J. Computer Network

    Description 给出一个图,求添加一条边使得添加后的图的桥(割边)最少. Sol Tarjan. 一遍Tarjan求割边. 我们发现连接的两个点一定是这两个点之间的路径上的桥最多,然后就可以贪 ...

  9. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

随机推荐

  1. angularjs笔记(二)

    AngularJS API 4.AngularJS过滤器 使用一个管道符(|)添加到表达式和指令中 例1.格式化字母转为大写 <!DOCTYPE html> <html> &l ...

  2. JavaWeb学习笔记——JSP标准标签库JSTL

  3. Nginx+PHP-fpm高负载优化及压力测试方法

    Nginx+PHP-fpm组合,以内存占用小,负载能力强壮的特点,成为小内存VPS建站的首选组合.我们一起来探讨一下nginx+php-fpm高负载的优化方法. 先来看看nginx配置参数的优化.ng ...

  4. 一张图总结docker命令

  5. C语言进行面向对象编程

    http://blog.csdn.net/dadalan/article/details/3983888 http://blog.163.com/zhqh43@126/blog/static/4043 ...

  6. group

    学习Linq时,经常会遇到Linq使用Group By问题,这里将介绍Linq使用Group By问题的解决方法. 1.计数 var q = from p in db.Products group p ...

  7. JavaScript 面向对象程序设计(下)——继承与多态 【转】

    JavaScript 面向对象程序设计(下)--继承与多态 前面我们讨论了如何在 JavaScript 语言中实现对私有实例成员.公有实例成员.私有静态成员.公有静态成员和静态类的封装.这次我们来讨论 ...

  8. Python开发【第十八篇】:MySQL(二)

    视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( SEL ...

  9. TCSQL实时列表缓存数据库帮助文档

    [文章作者:张宴 本文版本:v1.1 最后修改:2010.09.03 转载请注明原文链接:http://blog.zyan.cc/tcsql/] 曾经有人提出,一般数据库缓存分为四种.第一种:单个对象 ...

  10. PostgreSQL建立分区表示例

    pgsql 分区表: --主表 create table test(id integer, name varchar(32)); create index idx_test_id on test us ...