Selection (opentrains)
来源:互联网 发布:c stl 源码 编辑:程序博客网 时间:2024/06/07 00:50
这个本来是一道水题,唯一的坑就是shift只能操作一次
然后用f[i]记录前i个字符中 ‘*’ 的数量,g[i]记录前i个字符中 ‘.’ 的数量(当然也可以直接用i-f[i]运算)
假设当前位置为i,要[x,i]作为shift的区段,操作数为f[n]-f[i]+f[x-1]+g[i]-g[x-1]+2,可以转化一下成:f[n]-f[i]+g[i]+f[x-1]-g[x-1]+2,其中i为枚举变量,只要维护 f[x-1]-g[x-1] 尽量大就好了,最终是O(n)的
#include <bits/stdc++.h>#include <string.h>using namespace std;const int maxn = 100010;char s[maxn];int n, f[maxn], ans, t, l, x, y, g[maxn], sum;int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d", &n); scanf("%s", s+1); l = 1; x = 0; y = 0; for (int i = 1; i <= n; i++) { f[i] = f[i-1]; g[i] = g[i-1]; if (s[i] == '*') f[i]++; else g[i]++; } ans = f[n]; for (int i = 1; i <= n; i++) { if (i > 1) { //l, i if (f[n]-f[i]+g[i]-g[l-1]+f[l-1]+2 < ans) { ans = f[n]-f[i]+g[i]-g[l-1]+f[l-1]+2; x = l; y = i; } } if (f[i-1]-g[i-1] < f[l-1]-g[l-1]) l = i; } sum = 0; for (int i = 1; i < x; i++) if (s[i] == '*') sum++; for (int i = x; i <= y; i++) if (s[i] == '.') sum++; for (int i = y+1; i <= n; i++) if (s[i] == '*') sum++; if (x != 0) sum += 2; if (sum != ans) while (1) {} printf("%d\n", ans); bool flag = false; if (x != 0) { printf("%d\n", x); printf("Shift+%d\n", y); flag = true; } for (int i = 1; i < x; i++) if (s[i] == '*') { if (flag) printf("Ctrl+%d\n", i); else printf("%d\n", i); flag = true; } for (int i = x; i <= y; i++) if (s[i] == '.') { if (flag) printf("Ctrl+%d\n", i); else printf("%d\n", i); flag = true; } for (int i = y+1; i <= n; i++) if (s[i] == '*') { if (flag) printf("Ctrl+%d\n", i); else printf("%d\n", i); flag = true; }}
阅读全文
0 0
- Selection (opentrains)
- Sequence (opentrains)
- Bubble (opentrains)
- Maze (opentrains)
- Tree packing opentrains
- Blobs' Exhibition opentrains
- Selection
- Selection
- :: selection
- Selection sort
- type selection
- Selection Sort
- 7、Selection
- Selection sort
- feture selection
- model selection
- Selection Sort
- Selection Screen
- activemq入门基于queue和基于topic的demo
- 判断磁盘/U盘 是否 被写保护
- 分布式事务解决方案一之:可靠消息最终一致性
- IRremote类库中涉及的Container for received IR codes
- JS实现div不停旋转代码
- Selection (opentrains)
- git报错--RPC failed; curl 18 transfer closed with outstanding read data remaining
- js小数转换百分数
- 排序功能的实现
- 在QT对话模中用webengine 无法加载WEB页的问题
- 查看webjoin下的tomcat的版本
- 144. Binary Tree Preorder Traversal
- Android中用TextView显示大量文字的方法
- php视频上传