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
题意:找最少的连续页数使得覆盖全书的所有知识点
题解:尺取法,也叫做two point 很形象就是两段不断改变来找到最值。时间复杂度减低了很多
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int p,a[N];
set<int>ss;
map<int,int>m; int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
scanf("%d",&p);
for(int i=;i<p;i++)
{
scanf("%d",&a[i]);
ss.insert(a[i]);
}
int n=ss.size();
int num=,st=,en=,ans=p;
for(;;)
{
while(en<p&&num<n){
if(m[a[en++]]++==)num++;
}
if(num<n)break;
ans=min(ans,en-st);
if(--m[a[st++]]==)num--;
}
printf("%d\n",ans);
return ;
}

还有就是不知道为啥用cin和cout居然TLE了,而且我还加了

ios::sync_with_stdio(false);
cin.tie(0);

代码也放上来!!

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int p,a[N]; int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>p;
set<int>ss;
for(int i=;i<p;i++)
{
cin>>a[i];
ss.insert(a[i]);
}
int n=ss.size();
map<int,int>m;
int num=,st=,en=,ans=p;
for(;;)
{
while(en<p&&num<n){
if(m[a[en++]]++==)num++;
}
if(num<n)break;
ans=min(ans,en-st);
if(--m[a[st++]]==)num--;
}
cout<<ans<<endl;
return ;
}

poj3320尺取法的更多相关文章

  1. POJ3320 尺取法的正确使用法

    一.前言及题意: 最近一直在找题训练,想要更加系统的补补思维,补补漏洞什么的,以避免被个类似于脑筋急转弯的题目干倒,于是在四处找书,找了红书.蓝书,似乎都有些不尽如人意.这两天看到了日本人的白书,重新 ...

  2. poj3320(尺取法)

    题目大意:给你一串数字,找出最小的能够覆盖所有出现过的数字的区间长度: 解题思路:依旧是尺取法,但要用map标记下出现过的书: 代码:别用cin输入: #include<iostream> ...

  3. poj3320 (尺取法)

    n个数,求最小区间覆盖着n个数中所有的不相同的数字. 解题思路: AC代码: import java.util.HashMap; import java.util.HashSet; import ja ...

  4. 尺取法 poj3061 poj3320

    尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...

  5. poj3061 poj3320 poj2566尺取法基础(一)

    poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...

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

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

  7. 【尺取法】POJ3061 & POJ3320

    POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...

  8. 【转】毛虫算法——尺取法

    转自http://www.myexception.cn/program/1839999.html 妹子满分~~~~ 毛毛虫算法--尺取法 有这么一类问题,需要在给的一组数据中找到不大于某一个上限的&q ...

  9. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

随机推荐

  1. javascript继承---组合式继承

    原型链继承和构造函数继承中存在着很多的缺陷,所以,使用组合式继承了进行弥补这些缺陷 //组合继承 //父函数 function a(){ this.name = "兔子只吃胡萝卜" ...

  2. Button颜色选择器进阶

    drawable/button_color.xml <?xml version="1.0" encoding="utf-8"?> <selec ...

  3. 微信JS图片上传与下载功能--微信JS系列文章(三)

    概述 在前面的文章微信JS初始化-- 微信JS系列文章(一)中已经介绍了微信JS初始化的相关工作,接下来本文继续就微信JS的图片上传功能进行描述,供大家参考. 图片上传 $(function(){ v ...

  4. 对Golang有兴趣的朋友,推荐一款go语言Web框架-dotweb

    Go语言,2009年推出,对我个人,2015年下半年,才下定决心正式开始引入使用Go,自此,让我获得了一种全新的开发体验. 在不断的项目过程中,一个开发人员总喜欢堆积一些代码段,由于Go的开源特性,逐 ...

  5. ATS来了,网页HTTP访问怎么办?

    推荐理由 ATS(App Transport Security),是苹果在WWDC 15提出的,苹果将收紧http的访问,这样会造成我们周边的许多站点和应用均不能正常访问,这里就对ATS进行了简单分析 ...

  6. vue的使用总结

    1.vue的生命周期

  7. 疯狂的 JAVA 后++

    一.x++ 所以执行完x++之后,局部变量区的x值,直接为2: iinc: 指定int型变量增加指定的值,注意是变量,我的解释是iinc直接对局部变量操作,而不是对操作栈进行操作! ★★★★ OUTP ...

  8. MySQL(Navicat)运行.sql文件时报错[Err] 2006 - MySQL server has gone away 的解决方法

    在my.ini里加上  max_allowed_packet=16M

  9. 老李秘技:loadrunner11是否还支持dblib协议?

    老李秘技:loadrunner11是否还支持dblib协议?   Loadrunner11不再支持Sybase CTLIB 和 DBLIB协议 在loadrunner安装文件中找到*. LRP文件,位 ...

  10. 老李分享:为何要使用 Web Services

    老李分享:为何要使用 Web Services   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询q ...