首页 技术 正文
技术 2022年11月16日
0 收藏 408 点赞 4,453 浏览 863 个字

题目描述 Description

给你一个长度为n的数字串,数字串里会包含1-m这些数字。如果连续的一段数字子串包含了1-m这些数字,则称这个数字字串为NUM串。你的任务是求出长度最短的NUM串是什么,只需要输出这个长度即可。
1<=n,m<=200000

输入描述 Input Description

第一行给定n和m。 
第二行n个数,表示数字串,数字间用空格隔开。

输出描述 Output Description

如果存在NUM串则输出最短NUM串长度,否则输出“NO”。

样例输入 Sample Input

5 3
1 2 2 3 1

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

各个测试点1s

/*
设l和r序列的左端点和右端点,长度即为r-l+1,先使r增加,并且统计每个数字出现的次数,1~m都凑齐后,再使l增加,当一个数字出现1次以上时,就可以抛弃它,直到某个数字只出现过一次,一定要边循环边判断,因为在循环的过程中,不断有满足在1~m中的数字添进来,所以最优解是不断更新的。
*/
#include<cstdio>
#include<iostream>
#define M 200010
#define INF 0x3f3f3f3f
using namespace std;
int a[M],sum[M];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int l=,cnt=,ans=INF;
for(int r=;r<=n;r++)
{
if(a[r]<||a[r]>m)continue;
if(!sum[a[r]])cnt++;
sum[a[r]]++;
if(cnt==m)
{
for(int i=l;i<=n;i++)
{
if(sum[a[i]]>)
{
sum[a[i]]--;
l++;
}
else break;
}
ans=min(ans,r-l+);
}
}
if(cnt!=m)printf("NO");
else printf("%d",ans);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920