POJ 3320_Jessica's Reading Problem
2024-09-04 07:30:30
题意:
每页书都对应一个知识点,问最少看连续的多少页,才能把所有知识点都看完?
分析:
《挑战程序设计竞赛》介绍的尺取法,反复推进区间的开头和结尾,来求取满足条件的最小区间,先确定好一个满足条件的区间,然后不断往后移,找满足条件的区间。
代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<map>
#include<cstdio>
#include<vector>
#include<set>
using namespace std;
const int maxn = 1e6+5;
map<int, int>cnt;
int a[maxn];
set<int>all;
int main (void)
{
int P;scanf("%d",&P);
for(int i = 0; i < P; i++) {
scanf("%d",&a[i]);
all.insert(a[i]);
}
int si =all.size();
int last = 1, s = 0, res, num = 0;
for(int i = 0; i < P; i++){
if(cnt[a[i]]++==0) num++;
if(num==si){
s = i + 1;
break;
}
}
res = s;
while(last < P){
if(cnt[a[last-1]]==1){
while(s<P&&a[s]!=a[last-1]) cnt[a[s++]]++;
if(s==P) break;
else s++;
}else cnt[a[last-1]]--;
res = min(res, s - last);
last++;
}
printf("%d\n",res);
}
最新文章
- Goppa code
- Spring依赖注入三种方式详解
- HDU 4419 Colourful Rectangle(线段树+扫描线)
- hibernate使用原生SQL查询返回结果集的处理
- KMP与扩展KMP
- 序列化与反序列化的单例模式实现和readResolve()
- 使用Tomcat的Reload提高开发速度(翻译)
- 公司项目接触到了FormData,总结一下
- C#使用ES
- js 中导出excel 较长数字串会变成科学计数法
- Office 超级录屏如何旋转视频90度之后保存
- vue之指令系统
- Android开发使用软件
- 使用interface与类型诊断机制判断一个类型是否实现了某个方法
- Jsp遍历后台传过来的List
- RFID编码
- Ubuntu14.04下使用PPA安装php5.6,php7
- c++ 网络编程(七) LINUX下 socket编程 基于套接字的标准I/O函数使用 与 fopen,feof,fgets,fputs函数用法
- Mysql存储Emoji表情[为何utf8不能存储以及如何使Mysql能够存储Emoji表情]
- 修改Nginx与Apache上传文件大小限制