• 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
    ",max);
    53     }
    54     return 0;
    55 }
    
    
  • 相关阅读:
    如何关闭内存自动释放池ARC
    你怀疑过“温水煮青蛙”的故事吗
    程序员应该加入的3个QQ群
    简述Oracle 11g 新特性
    ViewState、UpdatePanel及控件OnPre之间的纠葛
    今天,我看到一组图解释“ 什么是博士?”
    Java将何去何从
    给新手朋友 推荐几本书(从C#入门到SQL及设计模式)
    最新版 智能电脑键盘屏幕全记录 免费下载
    C#中两个问号和一个问号
  • 原文地址:https://www.cnblogs.com/xiong-/p/3765340.html
Copyright © 2020-2023  润新知