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_i*a**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_i*a**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;
}
原文地址:https://www.cnblogs.com/fusiwei/p/11751949.html
- 牛客面经 |这可能不只是一篇面经
- 图的基本算法(BFS和DFS)
- C++STL中set的使用策略(详解)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)
- 设计模式六大原则(1):单一职责原则
- 设计模式六大原则(2):里氏替换原则
- Selenium2+python自动化72-logging日志使用
- Codeforces Round #395 (Div. 2)(A.思维,B,水)
- php实现图形计算器
- Selenium2+python自动化73-定位的坑:class属性有空格
- 华中农业大学第五届程序设计大赛网络同步赛题解
- Java构造方法与析构方法实例剖析
- 5.训练模型之利用训练的模型识别物体
- KMP算法学习(详解)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法