POJ - 3320 Jessica's Reading Problem(尺取法+STL)

题目链接:http://poj.org/problem?id=3320

题意:一本n页的书(有很多重复的页),要求连续最少页数把这本书每种页都包括进去,求出最少页数。

例如1 8 8 8 1

就1和8两种页,第一次1 8连续两页就把每种页都包括进去了。

尺取法:顾名思义就是一段一段的取。一开始一直往后取,符合条件,然后进行前面减少操作,不符合条件再往后取。

如此重复,得到最优的解。这道题目给出的页的标记好像有特别大的,直接用数组的话会爆炸。

试过用vector,但是对vector的操作太麻烦了。一开始里面没有值,所以对应输出的是一个随机大数。

然后++和--操作还无法成功,要单独拿出来+1。看网上大牛们都是用map,试了下map发现
map里面一开始就把每个对应地点的值初始化为0。再用一个set找到不相同页的数量,整道题就简单了。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <map>
 5 #include <set>
 6 using namespace std;
 7
 8 const int N=1000000+10;
 9 const int INF=0x3f3f3f3f;
10 int a[N];
11
12 int main(){
13     map <int,int> b;
14     set <int> s;
15     int n,temp;
16     scanf("%d",&n);
17     for(int i=0;i<n;i++) {scanf("%d",&a[i]);s.insert(a[i]);}
18     int num=s.size();
19     int sum=0,ans=INF;
20     int st=0,en=0;
21     while(1){
22         while(sum<num&&en<n){
23             if(b[a[en++]]++==0) sum++;
24         }
25         if(sum<num) break;
26         ans=min(ans,en-st);
27         if(--b[a[st++]]==0) sum--;
28     }
29     printf("%d\n",ans);
30     return 0;
31 }

POJ - 3320 Jessica's Reading Problem(尺取法+STL)

时间: 2024-11-08 11:38:35

POJ - 3320 Jessica's Reading Problem(尺取法+STL)的相关文章

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

poj 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Accepted: 2824 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 littl

POJ 3320 Jessica&#39;s Reading Problem 尺取法/map

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accepted: 2369 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 littl

尺取法 POJ 3320 Jessica&#39;s Reading Problem

题目传送门 1 /* 2 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 3 */ 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <algorithm> 8 #include <set> 9 #include <map> 10 using namespace std; 11 12 const int M

POJ 3320 Jessica&#39;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

poj 3320 Jessica&#39;s Reading Problem(尺取法)

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 au

(尺取法)POJ 3320 Jessica&#39;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

题解报告:poj 3320 Jessica&#39;s Reading Problem(尺取法)

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 au

poj 3320 Jessica&#39;s Reading Problem

题意:有n页书,每页有个编号为ci的知识点,求最小看连续的页数,其中包括所有的知识点 分析:n<=1e6,只能搞O(n)的解法,无非就是枚举起点和终点,尺取法正好适合这个想法,枚举一个起点,然后往后扫描到区间内包括所有的知识点,然后每次起点都往右移动一次,直到扫描到右边界也没有答案了,就跳出 程序跑了469ms,应该是map太慢,可以hash搞一波,也过了,但是可以离散化知识点的编号,最多有1e6个,空间换时间 #include<iostream> #include<cstdio&