ZOJ 3790 Consecutive Blocks(尺取法)
来源:互联网 发布:淘宝店运营工作职责 编辑:程序博客网 时间:2024/05/20 04:14
There 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's color 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 of the 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 the same 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 by a 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 11 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;}
- ZOJ 3790 Consecutive Blocks(尺取法)
- ZOJ 3790 Consecutive Blocks(迟取法)
- ZOJ 3790 Consecutive Blocks
- zoj-3790-Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks 贪心
- ZOJ月赛3790Consecutive Blocks(贪心)
- ZOJ 3790 Consecutive Blocks(枚举+二分查找)
- ZOJ 3790 Consecutive Blocks 模拟题
- ZOJ 3790 Consecutive Blocks 排序+扫描
- ZOJ 3790 Consecutive Blocks [排序+扫描]
- ZOJ 3790 Consecutive Blocks【离散化】【贪心】
- ZOJ Consecutive Blocks(左右指针移动)
- zoj 3790 Consecutive Blocks(链表重点是思想)
- Consecutive Blocks (ZOJ3970)
- iOS 9学习系列:如何使用ATS提高应用的安全性
- Gulp构建Android项目,安装APK
- Unity动态生成带碰撞体的LineRenderer
- 将Maven项目打包成可执行jar文件(引用第三方jar)
- 九度oj 1117
- ZOJ 3790 Consecutive Blocks(尺取法)
- day5: 字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序
- 黑马程序员——018——IO流③(读取/写入转换流(码表)、流操作规律、PrintStream、异常处理)
- iOS中OC字符串与C语言字符串之间的相互转换
- android权限管理
- WIN7 嵌入式系统安装教程 Windows Embedded Standard 2011 安装
- 堆排序
- 怎样打包java文件,使之能直接在系统下运行
- Java学习笔记·#1·Java环境搭建