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\n",ans);
32     return 0;
33 }
View Code

 

转载于:https://www.cnblogs.com/Blue233333/p/7608563.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值