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**ik ), 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≤dn ) 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≤dn ). The second line contains nn integers a_1, a_2, \dots, a_na1,a2,…,a**n ( 1 \le a_i \le k1≤a**ik ), 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)的更多相关文章

  1. CF1225B1 TV Subscriptions (Easy Version)

    CF1225B1 TV Subscriptions (Easy Version) 洛谷评测传送门 题目描述 The only difference between easy and hard vers ...

  2. 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 ...

  3. B2 - TV Subscriptions (Hard Version)

    题目连接:https://codeforces.com/contest/1247/problem/B2 题解:双指针,,一个头,一个尾,头部进入,尾部退出,一开始先记录1到k,并记录每个数字出现的次数 ...

  4. 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 ...

  5. [CodeForces-1225B] TV Subscriptions 【贪心】【尺取法】

    [CodeForces-1225B] TV Subscriptions [贪心][尺取法] 标签: 题解 codeforces题解 尺取法 题目描述 Time limit 2000 ms Memory ...

  6. cf--TV Subscriptions (Hard Version)

    time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standa ...

  7. 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. 题解:很显然只有 \( ...

  8. 28 自定义View流式布局

    流式布局每行的行高以本行中最高的元素作为高,如果一个元素放不下到一行时直接到第二行 FlowLayoutView package com.qf.sxy.customview05.widget; imp ...

  9. Android support library支持包常用控件介绍(二)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...

随机推荐

  1. Linux系统中的截图功能(类似QQ、微信、Snipaste截图功能)

    作者亲笔测试Ubuntu16.04,18.04,deepin15.11桌面版本Linux内核系统. 安装: 1. 终端命令黑框 2. sudo apt-get install flameshot(体积 ...

  2. ZOJ 3778 Talented Chief

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3778 题目 某人做菜很厉害,一分钟能同时完成最多m个菜的一道工序,输入菜的 ...

  3. 最小化MarkdownPad 2安装体积(win10)

    一.原因 MarkdownPad2在Win10当中可能无法正常运行,右侧预览界面会出现错误"This view has crashed!"查阅官网FAQ得知大多数情况下安装Awes ...

  4. mysql索引类型:FULLTEXT、NORMAL、SPATIAL、UNIQUE的详细介绍(转)

    Normal 普通索引 表示普通索引,大多数情况下都可以使用 Unique 唯一索引 表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique 约束唯一标识 ...

  5. HDL的三种描述方式

    结构化描述 结构化描述方式是最原始的描述方式,是抽象级别最低的描述方式,但同时也是最接近于实际的硬件结构的描述方式.结构化的描述方式,思路就像在面包板上搭建数字电路一样,唯一的不同点就是我们通过HDL ...

  6. activiti5初识

    因工作需要,接手新的项目,其中用到了activiti实现的工作流,特意去大致学习下,特此记录下. 1.acticiti5框架说明及表结构介绍 Activiti5工作流引擎框架: 它实际上是一个java ...

  7. 【linux命令】权限管理命令(chattr、lsattr、sudo)

    目录 chattr lsattr sudo 一.chattr命令 chattr命令用来修改文件系统的权限属性,只有 root 用户可以使用,建立凌驾于 rwx 基础权限之上的授权. PS:chattr ...

  8. 使用ScriptX控件实现IE浏览器分页打印功能

    之前讲过js调用ie浏览器自带打印的用法,今天讲使用插件的方式.浏览器自带打印不能控制页边距.页眉页脚等选项,尤其是如果分页打印的话,无法自动将前一页标题带到本页,所以不适用多页打印的功能.使用Scr ...

  9. 知识图谱如何运用于RecomSys

    将知识图谱作为辅助信息引入到推荐系统中可以有效地解决传统推荐系统存在的稀疏性和冷启动问题,近几年有很多研究人员在做相关的工作.目前,将知识图谱特征学习应用到推荐系统中主要通过三种方式——依次学习.联合 ...

  10. OpenGL光照1:颜色和基础光照

    本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...