Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

A very hard-working boy had manually indexed for her each page of Jessica's text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica's text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1

Sample Output

2

题意:

一本书有p页 每一页都有一个知识点ai 存在不同两页上的知识点相同的情况

求最少读连续的多少页书 能把所有的知识点全部覆盖到

尺取法:

反复的推进区间的开头和结尾 来求满足条件的最小区间的方法被称为尺取法

尺取法的复杂度为O(n).

题解:

初始化 start  end  sum =0;

然后推进end 直到恰好满足条件退出 如果区间已经到了结尾 仍不满足 则退出

然后更新答案 ans = min(ans,end-start);

然后start推进一位,要处理这时对sum的影响

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
const int N=1e6+;
const int mod=1e9+;
int a[N];
int main()
{
int n;
while(~scanf("%d",&n)){
map<int,int>mp;
int num=;
for(int i=;i<n;i++){
scanf("%d",a+i);
if(!mp[a[i]]) num++;
mp[a[i]]++;
}
int st=,t=,sum=,ans=n;
mp.clear();
while(){
while(t<n&&sum<num){
if(!mp[a[t++]]++)
sum++;
}
if(sum<num) break;
ans = min(ans,t-st);
if(--mp[a[st++]]==)
sum--;
}
printf("%d\n",ans);
}
return ;
}

POJ 3320 Jessica's Reading Problem (尺取法)的更多相关文章

  1. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...

  2. POJ 3320 Jessica's Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  3. poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用

    jessica's Reading PJroblem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9134   Accep ...

  4. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  5. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  6. POJ 3320 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6001   Accept ...

  7. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  8. 题解报告:poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  9. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

随机推荐

  1. 20171018 在小程序页面去获取用户的OpenID

    1. 在小程序的.js 文件中增加代码 //加载页面时到后台服务去获取openID onLoad: function (options) { //OpenId wx.login({ //获取code ...

  2. shell中expect介绍

    expect介绍 借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员 ...

  3. SSM基础整合

    1.表现层: 依赖jar包:spring+JSP+文件上传组件+dubbo+fastdfs web.xml配置:乱码过滤器+DispatcherServlet前端控制器 <!-- 配置post乱 ...

  4. c#中取整和取余

    "%"为取余. "/"号整型运算是取整,浮点运算时为除法运算.如54/10结果为5,54.0/10.0结果为5.4.而且取整时不进行四舍五入只取整数部分,如54 ...

  5. 关于RTSP-Over-HTTP

    --------------------更新2018.07.10------------------- myRtspClient-1.3.0开始支持RTSP-Over-HTTP ----------- ...

  6. Linux du命令详解

    1.命令:du 2.命令功能:显示每个文件和目录的磁盘使用空间. 3.命令参数 -a或-all #显示目录中个别文件的大小. -b或-bytes #显示目录或文件大小时,以byte为单位. -c或-- ...

  7. MongoDB--编译文件

    -j 加你的cpu核数来加速编译过程 编译出错可以自行安装所需依赖. 或者去官网直接下载编译好的二进制,直接执行. 编译好之后查看文件: mongod:mongodb的执行程序,数据库部署也用这个程序 ...

  8. 还是Go 为了伟大的未来

    今天,还是想讲讲Go 我觉得还没讲够,哈哈哈 其实,是想把框架再清晰些,因为上一篇框架没能引入goroutine(协程),感觉比较遗憾 下边,我就用上goroutine,但这里的协程仅是为了演示,没有 ...

  9. 8.C#友元程序集----可访问性相关

    在一个团队或者公司中,一个人开发出来的程序集中的类的可见性有public,private和internal, public:对所有程序集可见,就是说如果拿到程序集.DLL文件,那么类就被公开了. pr ...

  10. Xcode 运行 Signing for "XXXXXX" requires selecting either a development team or a provisioning profile. Select a development team or a provisioning profile in the project editor

    Signing for "XXXXXX" requires selecting either a development team or a provisioning profil ...