cf--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 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.
思路:
刚开始看到这个题目时,想到了莫队的分块统计+埃筛,写完了一直在debug,然后看了学长的代码明白过来了,原来是这样做
t组输入样例,
n表示n个数,k表示着n个数中的最大值,d表示连续的几个数中不同的数字个数(不重复的数字个数)
我可以表示出第一个区间里符合题意的树,然后依次往下移动,知道所有的都找完
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const int maxn = 1e6 + 10;
set<int> s;
map<int,int> mp;
int a[maxn];
int t,n,k,d;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--){
mp.clear();
s.clear();
cin >> n >> k >> d;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= d; i++){
mp[a[i]]++;
s.insert(a[i]);
}
//左右边界找到,然后依次下移,利用的map的计数和set的去重
int l = 1, r = d,ans = s.size();
while(r < n){//等于不成立,因为里面有r++
mp[a[l]]--;//我是想要右移
if(mp[a[l]]==0)//没有的话,就可以删除
s.erase(a[l]);
l++;
r++;
s.insert(a[r]);//向右移动,插入
mp[a[r]]++;//这一步刚开时忘记写,
if(s.size() < ans)
ans = s.size();
}
cout << ans << endl;
}
return 0;
}
cf--TV Subscriptions (Hard Version)的更多相关文章
- CF1225B2 TV Subscriptions (Hard Version)
CF1225B2 TV Subscriptions (Hard Version) 洛谷评测传送门 题目描述 The only difference between easy and hard vers ...
- CF1225B1 TV Subscriptions (Easy Version)
CF1225B1 TV Subscriptions (Easy Version) 洛谷评测传送门 题目描述 The only difference between easy and hard vers ...
- 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 ...
- B2 - TV Subscriptions (Hard Version)
题目连接:https://codeforces.com/contest/1247/problem/B2 题解:双指针,,一个头,一个尾,头部进入,尾部退出,一开始先记录1到k,并记录每个数字出现的次数 ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B. TV Subscriptions 尺取法
B2. TV Subscriptions (Hard Version) The only difference between easy and hard versions is constraint ...
- [CodeForces-1225B] TV Subscriptions 【贪心】【尺取法】
[CodeForces-1225B] TV Subscriptions [贪心][尺取法] 标签: 题解 codeforces题解 尺取法 题目描述 Time limit 2000 ms Memory ...
- 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. 题解:很显然只有 \( ...
- HBase 数据模型(Data Model)
HBase Data Model--HBase 数据模型(翻译) 在HBase中,数据是存储在有行有列的表格中.这是与关系型数据库重复的术语,并不是有用的类比.相反,HBase可以被认为是一个多维度的 ...
- OAF_文件系列5_实现OAF解析XML文件javax.xml.parsers(案例)
20150729 Created By BaoXinjian
随机推荐
- 2.7.1 元素定位:selenium消息框处理 (alert、confirm、prompt)
来源:http://blog.csdn.net/cui_angel/article/details/7784211 http://www.cnblogs.com/tobecrazy/p/ ...
- 10day 系统的selinux服务程序优化
selinux服务对root用户权限进行控制 很多企业中:selinux服务默认关闭 centos6==centos7 临时关闭: 检查确认: getenforce --- 确认selinux服务是否 ...
- 杭电ACM 1713 相遇周期
相遇周期 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- arcgis中的Join(合并连接)和Relate(关联连接)
arcgis中的Join(合并连接)和Relate(关联连接) 一.区别 1.连接关系不一样. Relate(关联连接)方式连接的两个表之间的记录可以是“一对一”.“多对一”.“一对多”的关系 Joi ...
- java 面试架构篇
1.非功能需求会考虑哪些? 可用性.扩展性.性能: 2.有没有遇到过建了索引反而变慢的情况? 3.从哪些角度去设计系统? 4.代码中使用过的设计模式?
- 每天进步一点点------Altium Designer Rules规则详解
对于PCB的设计, AD提供了详尽的10种不同的设计规则,这些设计规则则包括导线放置.导线布线方法.元件放置.布线规则.元件移动和信号完整性等规则.根据这些规则, Protel DXP进行自动布局和自 ...
- 每日扫盲(一):java的rmi
JAVA RMI 原理和使用浅析 本地对象调用 我们先看看本地对象方法的调用: ObjectClass objectA = new ObjectClass(); String retn = objec ...
- 解决MinGW运行时提示丢失libgmp-10.dll
如何解决刚安装的MinGW提示"丢失xxx.dll" 解决方案: 1.配置环境变量,出现这种问题的主要原因是因为没配置环境变量,网上的大多数解决方案也是基于这个的. 2.安装包损坏 ...
- css常见问题汇总
1. 如果我想显示两行文字第二行超出部分‘...’? 限制在一个块元素显示的文本的行数. -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit proper ...
- mapreduce课上实验
今天我们课上做了一个关于数据清洗的实验,具体实验内容如下: 1.数据清洗:按照进行数据清洗,并将清洗后的数据导入hive数据库中: 2.数据处理: ·统计最受欢迎的视频/文章的Top10访问次数 (v ...