题意:

每页书都对应一个知识点,问最少看连续的多少页,才能把所有知识点都看完?

分析:

《挑战程序设计竞赛》介绍的尺取法反复推进区间的开头和结尾,来求取满足条件的最小区间,先确定好一个满足条件的区间,然后不断往后移,找满足条件的区间。

代码:

#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); }

最新文章

  1. Goppa code
  2. Spring依赖注入三种方式详解
  3. HDU 4419 Colourful Rectangle(线段树+扫描线)
  4. hibernate使用原生SQL查询返回结果集的处理
  5. KMP与扩展KMP
  6. 序列化与反序列化的单例模式实现和readResolve()
  7. 使用Tomcat的Reload提高开发速度(翻译)
  8. 公司项目接触到了FormData,总结一下
  9. C#使用ES
  10. js 中导出excel 较长数字串会变成科学计数法
  11. Office 超级录屏如何旋转视频90度之后保存
  12. vue之指令系统
  13. Android开发使用软件
  14. 使用interface与类型诊断机制判断一个类型是否实现了某个方法
  15. Jsp遍历后台传过来的List
  16. RFID编码
  17. Ubuntu14.04下使用PPA安装php5.6,php7
  18. c++ 网络编程(七) LINUX下 socket编程 基于套接字的标准I/O函数使用 与 fopen,feof,fgets,fputs函数用法
  19. Mysql存储Emoji表情[为何utf8不能存储以及如何使Mysql能够存储Emoji表情]
  20. 修改Nginx与Apache上传文件大小限制

热门文章

  1. bootstrap框架栅格系统使用
  2. [BZOJ1257][CQOI2007]余数之和sum 数学+分块
  3. spring jdbc 批处理插入主健重复的数据
  4. vuex的应用和解决的实际问题
  5. vue2.0 路由传参(router-link传过去)
  6. VS2015 update3 安装 asp.net core 失败
  7. 认知升级x
  8. 数据库和java的类型转化
  9. java去左右的空格(包括全角空格,tab,回车等)
  10. C# 后台调用存储过程