zoj 3790 Consecutive Blocks 离散化+二分

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 }

 

 

转载于:https://www.cnblogs.com/xiong-/p/3765340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值