zoukankan      html  css  js  c++  java
  • BZOJ3048: [Usaco2013 Jan]Cow Lineup

    n<=100000个数,求删掉K种相同的数之后最长的相同数区间长度。

    原来是2指针裸题。两个指针,一个左边开始扫,一个右边找最长的区间,使得数字种数不超过K+1即可,然后统计答案。

    统计答案时,如果枚举左端点就用左端点更新答案,枚举右端点就用右。因为这个WA了两次。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 //#include<math.h>
     5 #include<algorithm>
     6 #include<iostream>
     7 using namespace std;
     8 
     9 int n,m;
    10 #define maxn 100011
    11 int a[maxn],li[maxn],cnt[maxn];
    12 int main()
    13 {
    14     scanf("%d%d",&n,&m);
    15     for (int i=1;i<=n;i++) scanf("%d",&a[i]),li[i]=a[i];
    16     sort(li+1,li+1+n);for (int i=1;i<=n;i++) a[i]=lower_bound(li+1,li+1+n,a[i])-li;
    17     int l=1,r=1,tot=0,ans=0;
    18     memset(cnt,0,sizeof(cnt));
    19     for (;l<=n;l++)
    20     {
    21         for (;r<=n;r++)
    22         {
    23             if (!cnt[a[r]]) tot++;
    24             if (tot>m+1) {tot--;break;}
    25             cnt[a[r]]++;
    26         }
    27         ans=max(ans,cnt[a[l]]);
    28         cnt[a[l]]--;
    29         if (!cnt[a[l]]) tot--;
    30     }
    31     printf("%d
    ",ans);
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    safeNet
    网店
    微信公众号自定义菜单与回车
    西游记对教育的启发
    zencart资源
    cmd批处理常用符号详解
    div垂直居中
    git工作量统计
    VS2012变化的快捷键:
    sql 树 递归
  • 原文地址:https://www.cnblogs.com/Blue233333/p/7608563.html
Copyright © 2011-2022 走看看