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. H5双重标题的适配

    在QQ和微信中会自代一个标题栏,而手机浏览器没有标题栏. 因此,我自己写了一个标题栏 <div class="headbar"> <center class=&q ...

  2. IT兄弟连 Java语法教程 赋值运算符

    从本书之前的内容中就一直在使用赋值运算符.现在是正式介绍赋值运算符的时候了.赋值运算符是单个等号”=“.在Java中,赋值运算符的工作方式与所有其它计算机语言相同.它的一般形式如下: var = ex ...

  3. c# Winform 继承窗体 无法拖动修改控件大小

    问题描述: 一个窗体集成父窗体,发现无法直接拖动修改的控件,比如修改大小等 特征: 不禁使父窗体控件,就算新加一个控件也会这样:鼠标放到控件移动手方块上会出现一个“继承的控件”的tooptip, 异常 ...

  4. MySQL(11)---约束

    MySQL(11)---约束 含义: 一种限制,用于限制表中的数据,为了保证表中的数据的准确和可靠性. 先把Mysql几种约束列出来: 主键约束 外键约束 唯一性约束 非空约束 默认值约束 自增约束 ...

  5. Python安装PyOpenGL

    1.目前PyOpenGL是用python2写的,如果你使用的是python3需要自己修改PyOpenGL,我这里使用的是python2.7 2.下载PyOpenGLhttps://pypi.org/p ...

  6. python3 对list对象的增删改查

    class peoples: people_list =[] class people: name='' age=-1 def __init__(self,name,age): self.name = ...

  7. 聚焦性能技术和实践, MTSC全面揭秘PerfDog演进之路

    商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 12月14日,2019年度中国移动互联网测试开发大会(Mobile Testing Summit China,简称 MTSC)深圳站于深 ...

  8. [转]Doing more with Outlook filter and SQL DASL syntax

    本文转自:https://blogs.msdn.microsoft.com/andrewdelin/2005/05/10/doing-more-with-outlook-filter-and-sql- ...

  9. 【LeetCode】198. 打家劫舍

    打家劫舍 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定 ...

  10. LCD RGB 控制技术 时钟篇(下)【转】

    上一篇博文,我们介绍了LCD RGB控制模式的典型时钟.那么这一片我们要详细的去讨论剩下的细节部分. 我们先回顾一下之前的典型时序图 在这个典型的时序图里面,除了上篇博文讲述的HSYNC VSYNC ...