CF1225B2 TV Subscriptions (Hard Version)
CF1225B2 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 kk TV shows. You know the schedule for the next nn days: a sequence of integers a_1, a_2, \dots, a_na1,a2,…,a**n ( 1 \le a_i \le k1≤a**i≤k ), where a_ia**i 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 \le d \le 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.
输入格式
The first line contains an integer tt ( 1 \le t \le 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 \le n \le 2\cdot10^51≤n≤2⋅105 , 1 \le k \le 10^61≤k≤106 , 1 \le d \le n1≤d≤n ). The second line contains nn integers a_1, a_2, \dots, a_na1,a2,…,a**n ( 1 \le a_i \le k1≤a**i≤k ), where a_ia**i 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\cdot10^52⋅105 .
输出格式
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.
输入输出样例
输入 #1复制
输出 #1复制
说明/提示
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.
题解:
滑动窗口题目的数据加强版(对比\(Easy\,\,\, Version\))
其实最初认识滑动窗口这种题的时候是学单调队列的时候。但是显然这道题不能用数据结构来做。那么我们回归滑动窗口的本质来尝试着做这道题。
滑动窗口的本质是啥?滑动啊!
那我们就可以进行模拟:先手动跑第一个线段(初始窗口),统计颜色出现的次数(存到\(cnt[i]\)数组,注意这个数组一定要开成\(10^6\).如果这个颜色是第一次出现(即\(cnt[i]=0\)),那么就把颜色数(\(sum\)变量)++)
然后开滑。每到一个新元素,先删除滑动之前最古老的那个元素。然后再把最新的元素加进来,然后看一看这个\(cnt[i]\)是否需要把\(sum\)也随着改动。然后就可以AC了。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2*1e5+10;
const int maxk=1e6+10;
int n,k,d;
int a[maxn];
int cnt[maxk],sum,ans;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(cnt,0,sizeof(cnt));
sum=0;
scanf("%d%d%d",&n,&k,&d);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=d;i++)
{
if(!cnt[a[i]])
sum++;
cnt[a[i]]++;
}
ans=sum;
for(int i=d+1;i<=n;i++)
{
cnt[a[i-d]]--;
if(!cnt[a[i-d]])
sum--;
if(!cnt[a[i]])
sum++;
cnt[a[i]]++;
ans=min(ans,sum);
}
printf("%d\n",ans);
}
return 0;
}
CF1225B2 TV Subscriptions (Hard Version)的更多相关文章
- 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 ...
- cf--TV Subscriptions (Hard Version)
time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standa ...
- 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. 题解:很显然只有 \( ...
- 28 自定义View流式布局
流式布局每行的行高以本行中最高的元素作为高,如果一个元素放不下到一行时直接到第二行 FlowLayoutView package com.qf.sxy.customview05.widget; imp ...
- Android support library支持包常用控件介绍(二)
谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...
随机推荐
- Day4- Python基础4 深浅拷贝、三目运算、列表生成式,迭代器&生成器、装饰器
本节内容: 1.深浅拷贝 2.三目运算 3.迭代器和生成器 4.装饰器 1.深浅拷贝 拷贝意味着对数据重新复制一份,深浅拷贝的含义就是:对于修改复制的数据是否会影响到源数据,拷贝操作对于基本数据结构需 ...
- Less(3)
1.先判断注入类型 (1)首先看到要求,要求传一个ID参数,并且要求是数字型的:?id=1 (2)再输入?id=1' 显示报错,报错信息多了一个括号,判断接收到的参数可能为id=('1') (3)输入 ...
- vue中简单的数据请求 fetch axios
fetch 不需要额外的安装什么东西,直接就可以使用 fetch(url, { method:'post', headers: { 'Content-Type': 'application/x-www ...
- 80道最新java基础部分面试题(六)
自己整理的面试题,希望可以帮到大家,需要更多资料的可以私信我哦,大家一起学习进步! 59.ArrayList和Vector的区别 答: 这两个类都实现了List接口(List接口继承了Collecti ...
- Ubuntu 16.04 安装 mujoco, mujoco_py 和 gym
Mujoco (1)官网(https://www.roboti.us/license.html)注册 license,教育邮箱注册可以免费使用一年.注意:一个邮箱账号只能供一台主机使用. 填写个人信息 ...
- python 学习(day1)
初识python python的创始人为吉多*范罗苏姆(Guido van Rossum).1989年圣诞节期间,开发出来的脚本解释程序. python是⼀⻔什么样的语言 python 是一门解释型语 ...
- 微信小程序反编译
看到一个有意思的小程序,想了解是如何实现的,于是找了反编译方法. 安装adb驱动 百度安装adb驱动, 设计设置开发者模式,连接电脑. -> % adb devices List of devi ...
- solidity定长数组和动态数组
固定长度的数组 固定长度数组声明 直接在定义数组的时候声明固定长度数组的值: uint[5] fixedArr = [1,2,3,4,5]; 可通过数组的length属性来获得数组的长度,进而进行遍历 ...
- Spring5源码解析2-register方法注册配置类
接上回已经讲完了this()方法,现在来看register(annotatedClasses);方法. // new AnnotationConfigApplicationContext(AppCon ...
- Hive_hdfs导入csv文件
转自:Hive_hdfs csv导入hive demo 1 create csv file.student.csv 4,Rose,M,78,77,76 5,Mike,F,99,98,98 2 pu ...