CSUST 2009-Longest Subarray(队列的应用)

题目链接:http://acm.csust.edu.cn/problem/2009
博客园食用链接: https://www.cnblogs.com/lonely-wind-/p/13397273.html
Description

You are given two integers C C C, K K K and an array of N N N integers a 1 , a 2 , ⋯   , a N a_1,a_2,\cdots,a_N a1,a2,,aNIt is guaranteed that the value of a i a_i ai is between 1 1 1 to C C C

We define that a continuous subsequence a l , a l + 1 , . . . , a r ( l ≤ r ) a_{l},a_{l+1},...,a_r(l \leq r) al,al+1,...,ar(lr) of array a is a good subarray if and only if the following condition is met:
∀ x ∈ [ 1 , C ] , ∑ i = l r [ a i = x ] ≤ K \forall x\in [1,C],\sum_{i=l}^{r}[a_i=x]\leq K x[1,C],i=lr[ai=x]K

It implies that if a number appears in the subarray, it will appear no greater than K K K times.

You should find the longest good subarray and output its length. Or you should print 0 0 0 if you cannot find any.

Input
Each case starts with a line containing three positive integers N , K , C ( 1 ≤ N , K , C ≤ 300000 ) N,K,C (1 \leq N,K,C \leq 300000) N,K,C(1N,K,C300000)

The second line contains N N N integer a 1 , a 2 , ⋯ a N ( 1 ≤ a i ≤ C ) a_1,a_2,\cdots a_N(1\leq a_i\leq C) a1,a2,aN(1aiC)

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,实际上挺简单的。

题目大意:让你找到一个最长的的连续子序列,使得这个子序列中每个元素出现的次数小于等于 K K K

既然是找连续的,那么这个就很简单了,一旦碰到出现次数大于 K K K的,我们将前面的元素一直往外抛就完事了,这不就是个队列的问题了嘛。

以下是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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值