B2. TV Subscriptions (Hard Version)

本文介绍了如何在BerTV频道中以最少的订阅数量观看连续d天的电视节目。问题涉及对节目播出日程的理解,以及如何在限定的天数内购买最少的节目订阅。给出了若干测试案例和解决方案,强调了采用在线算法的重要性。

链接:https://codeforces.com/contest/1247/problem/B2

The only difference between easy and hard versions is constraints.

The BerTV channel every day broadcasts one episode of one of the kk TV shows. You know the schedule for the next nn days: a sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k), where aiai is the show, the episode of which will be shown in ii-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 dd (1≤d≤n1≤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 ddconsecutive days in which all episodes belong to the purchased shows.

Input

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

The first line of each test case contains three integers n,kn,k and dd (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤1061≤k≤106, 1≤d≤n1≤d≤n). The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k), where aiai is the show that is broadcasted on the ii-th day.

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

Output

Print tt 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 ddconsecutive days. Please note that it is permissible that you will be able to watch more than dd days in a row.

Example

input

Copy

4
5 2 2
1 2 1 2 1
9 3 3
3 3 3 2 2 2 1 1 1
4 10 4
10 8 6 4
16 9 8
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3

output

Copy

2
1
4
5

Note

In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show 11 and on show 22. So the answer is two.

In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show.

In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows.

In the fourth test case, you can buy subscriptions to shows 3,5,7,8,93,5,7,8,9, and you will be able to watch shows for the last eight days.

题解:太菜了,数组开太大,又用了memset结果被hack了QAQ,这道题只能用On写过,所以就想到了找区间中有多少不同的节目

代码:

#include<bits/stdc++.h>
using namespace std;
int n,t,m,k,d;
int a[200001],b[1000001];
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d %d",&n,&k,&d);
		//memset(a,0,sizeof(a));
		//memset(b,0,sizeof(b));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			b[a[i]]=0;
		}
		int min1=k;
		int s=0;
		for(int i=1;i<=d;i++)
		{
			if(b[a[i]]==0)
			{
				s++;	
			}
			b[a[i]]++;
		}
		min1=s;
		for(int i=1;i<=n-d;i++)
		{
			b[a[i]]--;
			if(b[a[i]]==0)
			{
				s--;
			}
			if(b[a[i+d]]==0)
			{
				s++;
			}
			b[a[i+d]]++;
			min1=min(min1,s);
		}
		printf("%d\n",min1);
	}
}

 

帮我检查代码 const Stomp = require('@/static/js/stomp.js').Stomp; import config from '@/config.js'; class StompService { constructor() { this.client = null; this._initPromise = null; this._subscriptions = {}; this._retryCount = 0; this._maxRetryDelay = 60000; // 最多 60 秒 this._baseRetryDelay = 3000; this.socketOpen = false; this.socketMsgQueue = []; } subscribe(eventPath, cb) { this.init().then((client) => { const userCode = uni.getStorageSync('userInfo').code; const fullPath = `/user/${userCode}${eventPath}`; if (this._subscriptions[fullPath]) { this._subscriptions[fullPath].unsubscribe(); // 防止重复订阅 } const subscription = client.subscribe(fullPath, cb); this._subscriptions[fullPath] = subscription; }); } unsubscribe(eventPath) { const userCode = uni.getStorageSync('userInfo').code; const fullPath = `/user/${userCode}${eventPath}`; if (this._subscriptions[fullPath]) { this._subscriptions[fullPath].unsubscribe(); delete this._subscriptions[fullPath]; } } init() { if (this.client) return Promise.resolve(this.client); if (this._initPromise) return this._initPromise; this._initPromise = new Promise((resolve, reject) => { const ws = { send: (msg) => this.sendMessage(msg), onopen: null, onmessage: null, }; const token = uni.getStorageSync('token') || ''; const tokenType = uni.getStorageSync('tokenType') || 'Bearer'; const url = config.wsBaseUrl; const header = { Authorization: `${tokenType} ${token}`, }; wx.connectSocket({ url, header }); wx.onSocketOpen((res) => { console.log('WebSocket连接已打开!', res); this.socketOpen = true; this.socketMsgQueue.forEach((msg) => ws.send(msg)); this.socketMsgQueue = []; ws.onopen && ws.onopen(); }); wx.onSocketMessage((res) => { if (res && res.data) { let value = res.data; let code = value.charCodeAt(value.length - 1); if (code !== 0x00) { value += String.fromCharCode(0x00); res.data = value; } } ws.onmessage && ws.onmessage(res); }); const handleReconnect = () => { this.client = null; this.socketOpen = false; const delay = Math.min(this._baseRetryDelay * 2 ** this._retryCount, this._maxRetryDelay); console.warn(`WebSocket 断开,${delay / 1000}s 后重连...`); setTimeout(() => { this._retryCount++; this.init(); }, delay); }; wx.onSocketError((res) => { console.error('WebSocket 错误!', res); handleReconnect(); }); wx.onSocketClose((res) => { console.warn('WebSocket 已关闭!', res); handleReconnect(); }); Stomp.setInterval = (interval, f) => setInterval(f, interval); Stomp.clearInterval = (id) => clearInterval(id); const client = (this.client = Stomp.over(ws)); client.close = () => { wx.closeSocket(); }; client.connect( header, () => { console.log('STOMP connected'); this._retryCount = 0; // 成功后重置重连次数 resolve(client); }, (error) => { console.error('STOMP connect error', error); this.client = null; this.socketOpen = false; this._initPromise = null; reject(error); } ); }).finally(() => { this._initPromise = null; }); return this._initPromise; } sendMessage(message) { if (this.socketOpen) { wx.sendSocketMessage({ data: message }); } else { this.socketMsgQueue.push(message); } } disconnect() { if (this.client) { this.client.disconnect(() => { console.log('STOMP disconnected'); }); wx.closeSocket(); this.client = null; this._subscriptions = {}; this.socketOpen = false; } } } export default new StompService();
06-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值