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.

Example

input

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

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 1 and on show 2. 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,9, and you will be able to watch shows for the last eight days.

题意

有t组数据,每组数据给你n个a[i],1<=a[i]<=k,让你找一个连续长度为d的区间,是的这个区间里面重复的数最少,问你是多少

题解

叫做尺取法? 我们维护一个区间,加最右边的数,减去最左边的数,check不同大小的数目的增减即可

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int a[maxn];
int n,k,d;
void solve(){
scanf("%d%d%d",&n,&k,&d);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
map<int,int> H;
int ans = 1000000;
int now = 0;
for(int i=0;i<d;i++){
if(H[a[i]]==0){
now++;
}
H[a[i]]++;
}
ans=now;
for(int i=d;i<n;i++){
if(H[a[i-d]]==1){
now--;
}
H[a[i-d]]--;
if(H[a[i]]==0){
now++;
}
H[a[i]]++;
ans=min(ans,now);
}
cout<<ans<<endl;
} int main(){
int t;
scanf("%d",&t);
while(t--){
solve();
}
}

Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B. TV Subscriptions 尺取法的更多相关文章

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

    A - Forgetting Things 题意:给 \(a,b\) 两个数字的开头数字(1~9),求使得等式 \(a=b-1\) 成立的一组 \(a,b\) ,无解输出-1. 题解:很显然只有 \( ...

  2. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products

    链接: https://codeforces.com/contest/1247/problem/D 题意: You are given n positive integers a1,-,an, and ...

  3. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary

    链接: https://codeforces.com/contest/1247/problem/C 题意: Vasya will fancy any number as long as it is a ...

  4. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2. TV Subscriptions (Hard Version)

    链接: https://codeforces.com/contest/1247/problem/B2 题意: The only difference between easy and hard ver ...

  5. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things

    链接: https://codeforces.com/contest/1247/problem/A 题意: Kolya is very absent-minded. Today his math te ...

  6. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题

    F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...

  7. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) E. Rock Is Push dp

    E. Rock Is Push You are at the top left cell (1,1) of an n×m labyrinth. Your goal is to get to the b ...

  8. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things 水题

    A. Forgetting Things Kolya is very absent-minded. Today his math teacher asked him to solve a simple ...

  9. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products 数学 暴力

    D. Power Products You are given n positive integers a1,-,an, and an integer k≥2. Count the number of ...

随机推荐

  1. 大话设计模式Python实现-中介者模式

    中介者模式(Mediator Pattern):用一个对象来封装一系列的对象交互,中介者使各对象不需要显示地相互引用,从而使耦合松散,而且可以独立地改变它们之间的交互. 下面是一个中介者模式的demo ...

  2. Long类型数据前端精度丢失

    问题描述 后端把Long类型的数据传给前端,前端可能会出现精度丢失的情况.例如:201511200001725439这样一个Long类型的整数,传给前端后会变成201511200001725440 相 ...

  3. Seven Kinds of Testers - 七种类型的测试

    最近读了James大叔的一篇总结Tester类型的文章,获益良多.原文叫做Seven Kinds of Testers(链接:http://www.satisfice.com/blog/archive ...

  4. HTTP/2 新特性总结

    我在想了解HTTP/2的时候,查阅了很多资料,发现这篇很好,是外国的文章.我翻译过来,加入自己的一点理解. HTTP/2 更简单,高效,强大.它在传输层解决了以前我们HTTP1.x中一直存在的问题.使 ...

  5. ubuntu 库依赖问题

    安装apt-file, 可以查找各种库依赖的文件,或查找某个app的依赖库: sudo apt install apt-file apt-file update apt-flie search rpc ...

  6. ACR122U读卡器在win7以上系统使用过程中的设置项

    发现ACR122U这个读卡器在进行nested破解的时候总是卡死,换了N个驱动程序都不行. 后发现是windows系统因智能卡的即插即用设置导致的问题,可以通过组策略的设置搞定. gpedit.msc ...

  7. Android方法数超出限定的问题(multiDex,jumboMode)

    在Android项目开发中,项目代码量过大或通过引入很多jar导致代码量急剧增加,会出现错误: android.dex.DexIndexOverflowException: Cannot merge ...

  8. 【IDEA】(2)---MAC代码模版

    IDEA(2)-MAC代码模版 IDEA提供了许多的自带代码模版,这些模版主要是对于我们经常开发用到的代码制作成一个模版,比如for循环,这个是经常会用到的代码,如果没有代码模版,我们需要一个一个手动 ...

  9. python处理oracle数据库的返回数据

    上代码: import SqlHelper.ORACLE as ORA import pandas as pd if __name__ == '__main__': #连接数据库 ms = ORA.O ...

  10. Linux性能分析——分析系统性能相关的命令

    Linux性能分析——分析系统性能相关的命令 摘要:本文主要学习了Linux系统中分析性能相关的命令. ps命令 ps命令用来显示系统中进程的运行情况,显示的是当前系统的快照. 基本语法 ps [选项 ...