Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2

该博客讨论了Codeforces Round #596中的一道难题,即如何在硬版本的TV Subscriptions问题中,以最少的订阅数量观看连续d天的电视节目。题目要求在给定的节目日程表下,确定购买最少数量的节目订阅以便能连续观看d天。输入包括测试用例数量、天数n、节目总数k和连续观看天数d,以及接下来n天的节目安排。博主分享了自己因超时而被hack的经历,并提示读者直接查看代码即可理解解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B2 - TV Subscriptions (Hard Version)

The only difference between easy and hard versions is constraints.

The BerTV channel every day broadcasts one episode of one of the k TV shows. You know the schedule for the next n days: a sequence of integers a1,a2,…,an (1≤ai≤k), where ai is the show, the episode of which will be shown in i-th day.

The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately.

How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows d (1≤d≤n) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of d consecutive days in which all episodes belong to the purchased shows.

Input
The first line contains an integer t (1≤t≤10000) — the number of test cases in the input. Then t test case descriptions follow.

The first line of each test case contains three integers n,k and d (1≤n≤2⋅105, 1≤k≤106, 1≤d≤n). The second line contains n integers a1,a2,…,an (1≤ai≤k), where ai is the show that is broadcasted on the i-th day.

It is guaranteed that the sum of the values ​​of n for all test cases in the input does not exceed 2⋅105.

Output
Print t integers — the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for d consecutive days. Please note that it is permissible that you will be able to watch more than d days in a row.


一道思维题,本来就上去是过了的,但是之后被hack超时了;最后发现多memset一个数组了,多了500多ms;又tm掉分了;

在这里插入图片描述

这道题看代码就行;不需要解释

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=200100;
const LL mod=1e9+7;
int a[N];
int v[1000010];
int main(){
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--){
//		memset(a,0,sizeof(a));就是这组,多memset了 ; 
		memset(v,0,sizeof(v));
		int n,k,d;
		cin>>n>>k>>d;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		int mmin=1e9;
		int ans=0;
		for(int i=1;i<=d;i++){
			if(v[a[i]]==0) ans++;
			v[a[i]]++;
		}
		mmin=min(mmin,ans);
		int l=1;
		for(int i=d+1;i<=n;i++){
			v[a[l]]--;
			if(v[a[l]]==0) ans--;
			if(v[a[i]]==0) ans++;
			v[a[i]]++;
			l++;
			mmin=min(mmin,ans);
		}
		cout<<mmin<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值