Jessica’s Reading Problem

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 15224 Accepted: 5241

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


解题心得:

  1. 就是给你n个可能重复的数,要你选择选择一段最小的区间要包含所有的数,问这个最小的区间有多长。
  2. 其实就是一个尺取法啊,考验了一点点技巧,关于这段区间内有多少个数,可以使用map映射,在map中从0变1数量+1,从1变0数量-1,这个数列一共有多少个数可以使用set来区分。

#include <algorithm>
#include <stdio.h>
#include <map>
#include <set>
using namespace std;
const int maxn = 1e6+100; int num[maxn],n;
set <int> se;
map <int,int> maps; void init() {
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf("%d",&num[i]);
se.insert(num[i]);
}
} int main() {
init();
int sum = se.size();
int cnt,l,r,ans = maxn;
l = r = cnt = 0;
while(1) {
while(cnt < sum && r < n) {
if(maps[num[r]] == 0)
cnt++;
maps[num[r]]++;
r++;
}
if(cnt < sum)
break;
ans = min(ans,r-l);
maps[num[l]]--;
if(maps[num[l]] == 0)
cnt--;
l++;
}
printf("%d\n",ans);
}

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

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

  9. 题解报告: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 ...

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

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

随机推荐

  1. php支付走过的坑(支付宝篇 注册 秘钥 环境等等配置)

    支付这东西,说容易也容易,说难也难 代码这玩意还比较好说 但是 如果没有demo 直接去看官方文档 十有八九一脸懵逼 今天就整理一下 支付这块走过的坑 涉及 微信h5支付 支付宝h5支付 (api文档 ...

  2. Android 接入支付宝支付实现

    接上篇android接入微信支付文章,这篇我们带你来接入支付宝支付服务 简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址, ...

  3. [一个小问题]Mainfest配置文件的version问题小结

    问题起源自己安装好产品的新build,然后用自己本地编译出来的其中一个DLL去替换到产品中,本来以为可以正常执行,但是却发现这个新DLL无法被Load,无法被使用. 从Event Viewer里找到了 ...

  4. 【MATLAB】画信号频谱的子函数

    输入信号序列和采样率,该子函数可以画出该信号的频谱图. function [f,spec,NFFT]=spec_fft_plot(sample,L,Fs) % 输入数据说明: % sample:信号序 ...

  5. ansible使用9-Playbooks: Special Topics

    Accelerated Mode port 5099 持续连接30min --- - hosts: all accelerate: true tasks: - name: some task comm ...

  6. mybatis返回boolean值时数据库返回null

    Servlet.service() for servlet [springDispatcherServlet] in context with path [/ms] threw exception [ ...

  7. mybase修改内部文件免费使用

    关于mybase的介绍就不多说了,下载后一般只有30天的使用期限.以下方法可以无限次使用该软件(当然,每隔一个周期就需要修改myBase.ini) 原文博客详见:https://www.cnblogs ...

  8. oracle中文乱码问题

    1.查看服务器端编码select userenv('language') from dual;我实际查到的结果为:AMERICAN_AMERICA.ZHS16GBK 2.执行语句 select * f ...

  9. Node.js-ReferenceError: _filename is not defined

    简直不要被坑得太惨!!!你能?看出来这前面是两根下划线!两根下划线!两根下划线!太尴尬了~找了半天原因居然是这个!

  10. 画X,模拟水题

    题目链接:http://codeforces.com/contest/404/problem/A #include <stdio.h> #include <string.h> ...