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. [转载]或许您还不知道的八款Android开源游戏引擎

    或许您还不知道的八款Android开源游戏引擎         分类:             技术文章              2010-08-04 20:27     17430人阅读     ...

  2. 1.跨平台开发之~ VSCode开发第一个C程序

    VSCode的安装就不讲了,可以参考这个(http://www.cnblogs.com/dunitian/p/6661644.html) 写一个简单的C,然后F5运行,根据提示来配置文件 删掉前面的内 ...

  3. win7下nsis打包exe安装程序教程

    下载软件包: NSIS中文版 :https://pan.baidu.com/s/1mitSQU0 装好之后会出现两个软件:Nullsoft Install System 和 VNISEdit 编译环境 ...

  4. 老李推荐:第8章3节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动AndroidDebugBridge 3

    首先它通过查找JVM中的System Property来找到"com.android.monkeyrunner.bindir"这个属性的值,记得前面小节运行环境初始化的时候在mon ...

  5. Java并发基础:进程和线程之由来

    转载自:http://www.cnblogs.com/dolphin0520/p/3910667.html 在前面,已经介绍了Java的基础知识,现在我们来讨论一点稍微难一点的问题:Java并发编程. ...

  6. WebApp框架

    我所知道的webapp开发框架,欢迎补充, Framework7包含ios和material两种主题风格并且有vue版和react版, vue发现一个vue-material, react有一款mat ...

  7. Angularjs 动态添加指令并绑定事件

    先说使用场景,动态生成DOM元素并绑定事件,非常常见的一种场景,用jq实现效果: http://jsbin.com/gajizuyuju/edit?html,js,output var count=0 ...

  8. MyBetis3.2框架技术

    1.1    MyBatis介绍 MyBatis 世界上流行最广泛的基于SQ语句的ORM框架,由Clinton Begin 在2002 年创建,其后,捐献给了Apache基金会,成立了iBatis 项 ...

  9. npm 配置和安装 express4.X 遇到的问题及解决

    前言:懒得看前面两篇介绍的也可以从本节直接参考,但建议最好了解下,因为 4.X 的express 已经把命令行工具分离出来 (链接https://github.com/expressjs/genera ...

  10. Java基础学习(一)—方法

    一.方法的定义及格式 定义: 方法就是完成特定功能的代码块. 格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2){ 函数体; return 返回值; } 范例1: 写一个两个 ...