Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6496   Accepted: 1998

Description

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

Source

 
尺取法求解
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map> using namespace std; #define maxn 1000005 int p,len = ;
int a[maxn];
map<int,int> ele; void solve() {
map<int,int> vis;
int s = ,sum = ,pos = ;
int ans = p; for(; ; ++s) {
while(sum < len && pos <= p) {
if(vis.find(a[pos]) == vis.end() || vis[ a[pos] ] == ) {
++sum;
}
++vis[ a[pos++] ];
} if(sum < len) break;
if(--vis[ a[s] ] == ) {
--sum;
}
ans = min(ans,pos - s);
} printf("%d\n",ans);
} int main()
{
// freopen("sw.in","r",stdin); scanf("%d",&p);
for(int i = ; i <= p; ++i) {
scanf("%d",&a[i]);
if(ele.find(a[i]) == ele.end()) {
ele[ a[i] ] = len++;
}
}
//printf("len = %d\n",len); solve();
return ;
}

POJ 3320的更多相关文章

  1. 【尺取】POJ 3320

    POJ 3320 Jessica's Reading Problem 题意:一本书P页,第i页有ai知识点,问你至少从某一处开始连续要翻多少页才能复习完所有的知识点,不能跨页翻. 思路:<挑战程 ...

  2. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

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

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

  4. A - Jessica's Reading Problem POJ - 3320 尺取

    A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...

  5. POJ 3320 (尺取法+Hash)

    题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...

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

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

  7. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  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. poj 3320(尺取法)

    传送门:Problem 3320 参考资料: [1]:挑战程序设计竞赛 题意: 一本书有 P 页,每页都有个知识点a[i],知识点可能重复,求包含所有知识点的最少的页数. 题解: 相关说明: 设以a[ ...

随机推荐

  1. Ruby Code Style

    这篇博文逐渐写一点关于Ruby的编码风格,主要参考ruby-stytle-guide. 一些命名准则 函数与变量 使用蛇形小写比较好:some_var, some_function 目录和文件也使用s ...

  2. EMVTag系列5《8E 持卡人验证方法(CVM)列表》

    L: var. up to 252 -R(需求):数据必须存在,在读应用数据过程中,终端不检查 按照优先顺序列出卡片应用支持的所有持卡人验证方法 注:一个应用中可以有多个CVM列表,例如一个用于国内交 ...

  3. 社保系列11《ATR》

    1)  冷复位(Cold Reset) 当IC卡的电源电压和其他信号从静止状态中复苏且申请复位信号时,IC卡产生的复位. 2)  热复位(Warm Reset) 在时钟(CLK)和电源电压(VCC)处 ...

  4. Linux下如何查看JDK安装路径

    1:echo $JAVA_HOME 使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$JAVA_HOME,否则如下所示,根本定位不到JDK的安装路径 [root@localho ...

  5. Tutorial: Importing and analyzing data from a Web Page using Power BI Desktop

    In this tutorial, you will learn how to import a table of data from a Web page and create a report t ...

  6. ios中怎么样设置drawRect方法中绘图的位置

    其中drawRect方法中的参数rect就是用来设置位置的,

  7. SQL Server 2008 R2 主从数据库同步

    一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2    DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...

  8. 3.Knockout.Js(属性绑定)

    前言 让visible绑定到DOM元素上,使得该元素的hidden或visible取决于绑定的值. 简单的绑定 首先还是先定义一个ViewModel var AppViewModel = { shou ...

  9. R语言连接MYSQL

    操作系统:centos 6.4 64bit R语言可以使用RMySQL来连接Mysql数据库,直接使用数据库里面的数据生成图像. 这个是RMYSQL的说明: http://cran.r-project ...

  10. Linux获取线程tid线程名

    Linux获取线程tid线程名 1 2 3 4 5 6 //thread name char cThreadName[32] = {0}; prctl(PR_GET_NAME, (unsigned l ...