求固定长度的所有连续区间中不同数的个数的最小值(o(n))

B2. TV Subscriptions (Hard Version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 dd consecutive 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 dd consecutive 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.

题意:就是买票看电影,电影有n个插曲,每个插曲有一个编号,告诉你n天中每天对应的插曲的编号,买一张票后就可以看对应编号的所有插曲(不管出现在哪天都能看,一张票可以看无限多场该编号的插曲);

      如:n个数分别为1 3 3 2 4 3 6 买了编号为3的这一张票后,在第2,3,6天都能看

问:想要连续d天都能看电影,至少要买几张票,t组测试数据,每组第一行给出三个数n,k,d,分别代表n个数,n个数的值(即:每个插曲的编号)的上限,想要连续看n天(即:固定的连续区间的长度),接下来的第二行给出n天每天对应的插曲编号。

思路:这实际上就是求在长度为n的序列中所有长度为d的连续区间中的不同的数的个数的最小值。因为区间是连续的且长度固定,如n=6,d=2的所有连续区间为【1,2】,【2,3】,【3,4】,【4,5】,【5,6】,可以发现所有区间的左端点分别是1,2,3,4,5(连续递增的等差序列且公差为1),右端点同理。所以区间右端点从左往右依次遍历,每次删去上个区间的左端点即可得下一个区间,可以用一个指针指向上个区间的左端点(每次要删的元素的位置),右端点从左往右依次遍历,当记录下一个区间不同数的个数时,该指针指向的位置随右端点位置+1同时+1即可得下一个区间,然后要维护新的区间不同数的个数,将上个区间左端点的值对应的个数-1即可,这样就实现了o(n)地遍历固定长度的所有连续区间同时得到固定长度的所有连续区间中不同数的个数的最小值。

完整代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
map<int,int>mp;//记录每个数出现的次数
int t,n,k,d,a[maxn];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n>>k>>d;
        memset(a,0,sizeof(a));
        mp.clear();
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int cnt=0,minn=inf,l=1;//cnt记录当前区间不同数的个数,minn取固定长度的所有连续区间中不同数的个数的最小值,l为指向上个区间的左端点(每次要删的元素的位置)的指针
        for(int i=1;i<=n;i++)
        {
            if(mp[a[i]]==0){
                cnt++;
            }
            mp[a[i]]++;
            if(i==d){
                minn=min(minn,cnt);
            }
            else if(i>d){//每次删去上个区间的左端点即可得下一个区间
                mp[a[l]]--;//将上个区间左端点的值对应的个数-1,即删去上个区间的左端点位置上的那一个数
                if(mp[a[l]]==0)
                {
                    cnt--;//如果上一句减完后那个数对应的个数为零了(即那个数删没了,那该区间不同的数的个数要-1)
                }
                l++;//下一次要删的元素的位置+1(即:该区间的左端点(也是下一个区间对应的上一个区间的左端点))
                minn=min(minn,cnt);
            }
        }
        cout<<minn<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值