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 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
题意:
一本书有p页 每一页都有一个知识点ai 存在不同两页上的知识点相同的情况
求最少读连续的多少页书 能把所有的知识点全部覆盖到
尺取法:
反复的推进区间的开头和结尾 来求满足条件的最小区间的方法被称为尺取法
尺取法的复杂度为O(n).
题解:
初始化 start end sum =0;
然后推进end 直到恰好满足条件退出 如果区间已经到了结尾 仍不满足 则退出
然后更新答案 ans = min(ans,end-start);
然后start推进一位,要处理这时对sum的影响
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
const int N=1e6+;
const int mod=1e9+;
int a[N];
int main()
{
int n;
while(~scanf("%d",&n)){
map<int,int>mp;
int num=;
for(int i=;i<n;i++){
scanf("%d",a+i);
if(!mp[a[i]]) num++;
mp[a[i]]++;
}
int st=,t=,sum=,ans=n;
mp.clear();
while(){
while(t<n&&sum<num){
if(!mp[a[t++]]++)
sum++;
}
if(sum<num) break;
ans = min(ans,t-st);
if(--mp[a[st++]]==)
sum--;
}
printf("%d\n",ans);
}
return ;
}
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(尺取法)
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++)以后,尾指针 ...
随机推荐
- 前端 HTML 常用标签 head标签相关内容 script标签
script标签 定义JavaScript代码 <!--定义JavaScript代码--> <script type="text/javascript"> ...
- 如何将finecms链接URL中的list和show去掉
finecms上手还算比较快吧,对seo关注的朋友会想着将它的url改造了,里面多了-list-和-show-,可以直接去掉,下面就随着ytkah一起来进行设置吧. 首先到后台的url规则,将列表和列 ...
- 帝国cms添加修改会员字段时字段名不能带数字,否则注册页会出现空白
这几天ytkah在整帝国cms会员模块,根据客户需求添加不同的字段,这个相对不难,可还是遇到了点问题.当时添加会员字段时,在字段名用数字“1”来代表第一次,如下图的字段名“1rwsdy” 但是添加以后 ...
- vue中自定义软键盘
https://segmentfault.com/a/1190000012568480
- spark on yarn任务提交缓慢解决
1.为什么要让运行时Jar可以从yarn端访问spark2以后,原有lib目录下的大JAR包被分散成多个小JAR包,原来的spark-assembly-*.jar已经不存在 每一次我们运行的时候,如果 ...
- 以太坊如何使用CPU挖矿?
CPU挖掘 你可以用电脑的中央处理器(CPU)挖以太币.自从GPU矿工的效率高出两个数量级,它就不再盈利了.然而你可以用CPU挖掘在Morden测试网或私有链上挖矿,以便创建你测试合约和交易所需要的以 ...
- (转)以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘
什么是以太坊(Ethereum) 以太坊(Ethereum)是一个基于区块链技术,允许任何人构建和使用去中心化应用的区块链平台.像比特币一样,以太坊是开源的,并由来自全世界的支持者们共同维护.与比特币 ...
- 20165321实验一 Java开发环境的熟悉
一.idea调试: 二.实验内容: 实现学生成绩管理功能(增删改,排序,查找),并进行测试(正常情况,异常情况,边界情况). 运行截图: 代码: public class Student { Stri ...
- python timeit
//有时候,我们想知道一个函数的计算耗时,那么,你可以用timeit //test.py 1 import timeit 2 3 def func(): 4 s = 0 5 ...
- HP-UX平台Oracle启动实例遭遇:ORA-27154,ORA-27300,ORA-27301,ORA-27302
环境:HP-UX 11.31 + Oracle 11.2.0.4 现象:在hpux安装Oracle,按业务需求配置参数后,无法启动实例. 报错如下: ORA-27154:post/wait creat ...