POJ:3320-Jessica's Reading Problem(尺取法)
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
解题心得:
- 就是给你n个可能重复的数,要你选择选择一段最小的区间要包含所有的数,问这个最小的区间有多长。
- 其实就是一个尺取法啊,考验了一点点技巧,关于这段区间内有多少个数,可以使用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(尺取法)的更多相关文章
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- 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 ...
- poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用
jessica's Reading PJroblem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9134 Accep ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3320 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- 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 ...
- 题解报告: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 ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
随机推荐
- 微信小程序电商实战-商品列表流式布局
今天给大家分享一下微信小程序中商品列表的流式布局方式,根据文章内容操作就可以看到效果哦~~~ 流式布局概念 流式布局也叫百分比布局 把元素的宽,高,margin,padding不再用固定数值,改用百分 ...
- jQuery实现焦点图[兼容ie7+]
HTML: <div class="freehand" id="freehand"> <h1>宠物手绘</h1> <d ...
- (六)JavaScript之[Regular Expression]与[错误(try, catch, throw)]
10].正则表达式 /** * 正则表达式(Regular Expression): * * 用于文本搜索和文本替换 * */ /** * /good/i是一个正则表达式. * good是一个模式(用 ...
- 秒懂JSON.parse()与JSON.stringify()的区别
在网站开发中,Json是最为常见的一种数据交互手段.在使用过程中,常会遇到Json字段串和对象之间进行转换.很多朋友对于JSON.parse() 和JSON.stringify() 的区别,下面为大家 ...
- RAL调用
[RPC的定义].RPC的全称为 Remote Procedure Call(远程过程调用).远程过程调用是一个计算机通信协议.该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额 ...
- C4C销售订单中业务伙伴的自动决定功能Partner determination procedure
例子:我新建一个Sales Order,account 字段选择ID为1001的Account:Porter LLC 创建成功后,观察这个Sales Order的Involved Party里,Bil ...
- 【HHHOJ】ZJOI2019模拟赛(十五)03.17 解题报告
点此进入比赛 得分: \(42+10+14=66\) 排名: \(Rank\ 3\) \(Rating\):\(+53\) \(T1\):[HHHOJ200]稗田的梦中之梦(点此看题面) 暴力\(DF ...
- ORA-01262,oracle启动报错,及Oracle启动原理
错误状态: SQL> startup ORA-01261: Parameter db_recovery_file_dest destination string cannot be transl ...
- 【洛谷P1090】合并果子
合并果子 题目链接 贪心:每次先合并最小的两堆果子 用堆实现 #include<iostream> #include<cstdio> using namespace std; ...
- ES6学习(三):数组的扩展
chapter08 数组的扩展 8.1 扩展运算符 8.1.1 扩展运算符的含义 ... 如同rest运算符的逆运算,将一个数组转换为用逗号分隔的参数序列. console.log(...[1, 2, ...