ZOJ 3790 Consecutive Blocks
来源:互联网 发布:高级编程语言有哪些 编辑:程序博客网 时间:2024/05/30 02:53
离散化+暴力
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
Author: LIN, Xi
Source: ZOJ Monthly, June 2014
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <vector>using namespace std;const int maxn=100100;int n,k,c[maxn],nc,ans;vector<int> vi[maxn];void gaoit(int x){ int sz=vi[x].size(); if(sz<=ans) return; int L=0,nowsize=1; ans=max(ans,nowsize); for(int i=1;i<sz;i++) { nowsize=i-L+1; while(vi[x][i]-vi[x][L]+1-nowsize>k) { L++; nowsize=i-L+1; } ans=max(nowsize,ans); }}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { map<int,int> mp; nc=1;ans=0; for(int i=0;i<n;i++) { int a; scanf("%d",&a); if(mp[a]==0) mp[a]=nc++; c[i]=mp[a]; vi[i+1].clear(); } for(int i=0;i<n;i++) vi[c[i]].push_back(i); for(int i=1;i<nc;i++) gaoit(i); printf("%d\n",ans); } 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 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 Consecutive Blocks(左右指针移动)
- ZOJ Monthly, June 2014 C - Consecutive Blocks
- 程序猿如何做好职业规划
- 关于类ACM机试中输入类型的整理
- java自学之路-----反射机制 正则表达式
- Android开源项目SlidingMenu深入剖析
- struts2 自定义局部类型转换器
- ZOJ 3790 Consecutive Blocks
- 软文编辑技巧
- java.lang.ClassNotFoundException: ch.qos.logback.ext.spring.web.LogbackConfigListener
- [编程之美] 2.14 求数组的子数组之和的最大值
- win7下怎么安装IIS
- 利用C++对象确定性析构的原则来解析单例模式
- 经济学12级3、4、5、6班网络技术最后一课
- python学习笔记(1)----并发与IO
- MySQL安装图解