题目链接:http://acm.csust.edu.cn/problem/2009
博客园食用链接: https://www.cnblogs.com/lonely-wind-/p/13397273.html
Description
You are given two integers , and an array of integers It is guaranteed that the value of is between to
We define that a continuous subsequence
of array a is a good subarray if and only if the following condition is met:
It implies that if a number appears in the subarray, it will appear no greater than times.
You should find the longest good subarray and output its length. Or you should print if you cannot find any.
Input
Each case starts with a line containing three positive integers
The second line contains integer
Output
For each test case, output one line containing an integer denoting the length of the longest good subarray.
Sample Input 1
5 1 3
1 2 3 1 2
Sample Output 1
3
emmm,CSUSTOJ为数不多的几道英文题面QWQ,实际上挺简单的。
题目大意:让你找到一个最长的的连续子序列,使得这个子序列中每个元素出现的次数小于等于 。
既然是找连续的,那么这个就很简单了,一旦碰到出现次数大于 的,我们将前面的元素一直往外抛就完事了,这不就是个队列的问题了嘛。
以下是AC代码:
#include <bits/stdc++.h>
using namespace std;
const int mac=3e5+10;
int vis[mac],a[mac];
int main(int argc, char const *argv[])
{
int n,k,c;
scanf ("%d%d%d",&n,&k,&c);
for (int i=1; i<=n; i++)
scanf ("%d",&a[i]);
int ans=0,j=1;
for (int i=1; i<=n; i++){
vis[a[i]]++;
while (vis[a[i]]>k){
vis[a[j]]--; j++;
}
ans=max(ans,i-j+1);
}
printf("%d\n",ans);
return 0;
}