首页 > 技术文章 > zoj 3790 Consecutive Blocks 离散化+二分

xiong- 2014-06-03 11:02 原文

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 ≤ KN) 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 1
1 1 1 2 2 3 2 2

Sample Output

4

题目大意:给一串连续的数字,最多可以删除k个,求相同数字连续个数最大的值。
可以离散化一下把相同数字的下标Push到同一个容器中,再去每个容器中暴力求最大值。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn=100010;
 9 vector<int> v[maxn];
10 int f[maxn],X;
11 int test[maxn];
12 
13 int Max(int a,int b){ return a>b?a:b;}
14 
15 int fun(int a,int b)
16 {
17     int i,j,len=v[a].size();
18     for(i=len;i>=2;i--)
19     {
20         for(j=0;j<=len-i;j++)
21         {
22             if(v[a][i+j-1]-v[a][j]+1-i<=b)
23                 return i;
24         }
25     }
26     return 1;
27 }
28 
29 int main()
30 {
31     int n,k,i,max;
32     while(~scanf("%d %d",&n,&k))
33     {
34         for(i=0;i<n;i++) v[i].clear();
35         for(i=0;i<n;i++) 
36         {
37             scanf("%d",f+i);
38             test[i]=f[i];
39         }
40         sort(test,test+n);
41         X=unique(test,test+n)-test;
42         for(i=0;i<n;i++)
43         {
44             int t=lower_bound(test,test+X,f[i])-test;
45             v[t].push_back(i);
46         }
47         max=1;
48         for(i=0;i<X;i++)
49         {
50             max=Max(max,fun(i,k));
51         }
52         printf("%d\n",max);
53     }
54     return 0;
55 }

 


推荐阅读