ZOJ 3790 Consecutive Blocks(尺取法)
2015-08-21 18:17
801 查看
Consecutive BlocksTime Limit: 2 Seconds Memory Limit: 65536 KBThere are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in a row. And the i-th block'scolor is Ci (1 ≤ Ci ≤ 109). Now you can remove at most K (0 ≤ K ≤ N) blocks, then rearrange the blocks by their index from left to right. Please figure out the length ofthe largest consecutive blocks with the same color in the new blocks created by doing this.For example, one sequence is {1 1 1 2 2 3 2 2} and K=1. We can remove the 6-th block, then we will get sequence {1 1 1 2 2 2 2}. The length of the largest consecutive blocks with thesame color is 4.
Input
Input will consist of multiple test cases and each case will consist of two lines. For each test case the program has to read the integers N and K, separated bya blank, from the first line. The color of the blocks will be given in the second line of the test case, separated by a blank. The i-th integer means Ci.Output
Please output the corresponding length of the largest consecutive blocks, one line for one case.Sample Input
8 1 1 1 1 2 2 3 2 2
Sample Output
4
题意:求在n个数的数列中最多去掉k个数能得到的连续相同数字的最大数量。
思路:这题有很多方法,刚学了尺取法,我就用尺取法做,其中循环结束条件应为是s == n - 1 && t == n,开始我设为t == n,wa了几发。
#include<cstdio>#include<algorithm>#include<map>using namespace std;int a[100010];int main(){int n, K;while(~scanf("%d%d", &n, &K)){map<int, int> coun;for(int i = 0; i < n; i++){scanf("%d", &a[i]);}int res = 0, s = 0, t = 0, k = 0;for(;;){while(k < K && t < n ){if(a[t] != a[s]){k++;coun[a[t++]]++;}else{coun[a[t++]]++;}}while(k == K && t < n && a[t] == a[s]){coun[a[t++]]++;}res = max(res, coun[a[s]]);if(s == n - 1 && t == n) break; //结束条件应为 s == n - 1 && t == n 而不是只有 t == n;if(a[s] == a[s + 1]){coun[a[s]]--;s++;}else{coun[a[s]]--;s++;k = (t - s) - coun[a[s]];}}printf("%d\n", res);}return 0;}
相关文章推荐
- 经常访问的技术网站
- editor does not contain a main type 解决方法
- Delphi文件映射
- HBase初探
- cstring和相关类型的相互转换int,double,char*
- 第1章 unix基础知识
- 交易应用及网站驱动不兼容Windows 10的解决方案
- Delphi中ADO异步执行方式
- 读Java Concurrency in Practice. 第六章.
- 关于数据库索引
- 如何在“一周内”摸清一个行业
- Struts2学习笔记——02
- Spark修炼之道(基础篇)——Linux大数据开发基础:第一节、Linux介绍、安装及使用初步
- 创建com服务器
- 【感恩教师节】51CTO学院疯狂大让利活动
- input验证是否为有效手机号码,亲测可用
- 黑马程序员-----GUI
- C#实现由四周向中心缩小的窗体退出特效
- Spring MVC 中的基于注解的 Controller
- iOS 去掉导航栏的边界灰线;去掉搜索框的边界灰线和其中文本输入框的阴影