zoukankan      html  css  js  c++  java
  • 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 }
    
    
  • 相关阅读:
    XSS跨站脚本攻击
    TCPDUMP
    使用adb命令抓取崩溃日志
    Android ADB 5037端口被占用问题解决办法:改端口号
    Fedora 29安装 和配置
    给linux 增加软件图标
    新手上路教程5_安全管理
    shell基础笔记
    新手上路教程4_DQL语言
    MySQL 和SQLserver 创建表注释字段的差别
  • 原文地址:https://www.cnblogs.com/xiong-/p/3765340.html
Copyright © 2011-2022 走看看