Jessica's Reading Problem POJ 3320(尺取)

原题

题目链接

题目分析

题目要求一段连续的书页,里面包括了全部知识点,可以考虑尺取,由于知识点的编号无规律,可以考虑用set来记录全部知识点的种数,尺取的过程可以考虑用map来辅助判断区间[s,t]是否包括全部知识点,映射map<知识点编号,个数>,当每种知识点个数至少为1的时候表明[s,t]包含所有知识点.

代码

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <utility>
 6 #include <ctime>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <string>
10 #include <stack>
11 #include <queue>
12 #include <vector>
13 #include <set>
14 #include <map>
15
16 using namespace std;
17 typedef unsigned long long ULL;
18 typedef long long LL;
19 typedef long double LB;
20 const int INF_INT=0x3f3f3f3f;
21 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
22
23 set<int> con;
24 map<int,int> barrel;
25 int num[1000000];
26
27 int main()
28 {
29 //    freopen("testdata.in","r",stdin);
30 //  freopen("std.out","w",stdout);
31     int N;
32     cin>>N;
33     for(int i=0;i<N;i++)
34         scanf("%d",&num[i]),con.insert(num[i]);
35     int n=con.size();
36     int l=0,r=0,cnt=1;barrel[num[0]]++;
37     int ans=INF_INT;
38     while(true)
39     {
40
41         while(r<N-1&&cnt<n)
42         {
43             r++;
44             if(barrel[num[r]]==0)
45                 cnt++;
46             barrel[num[r]]++;
47         }
48         if(cnt<n) break;
49         ans=min(ans,r-l+1);
50         barrel[num[l]]--;
51         if(barrel[num[l]]==0) cnt--;
52         l++;
53     }
54     printf("%d\n",ans);
55     return 0;
56 }

Jessica's Reading Problem POJ 3320(尺取)

原文地址:https://www.cnblogs.com/VBEL/p/11729107.html

时间: 2024-12-17 16:54:32

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

Jessica&#39;s Reading Problem(POJ 3320)

原题如下: Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16925   Accepted: 5861 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 spen

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

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

题目链接:http://poj.org/problem?id=3320 题意:一本n页的书(有很多重复的页),要求连续最少页数把这本书每种页都包括进去,求出最少页数. 例如1 8 8 8 1 就1和8两种页,第一次1 8连续两页就把每种页都包括进去了. 尺取法:顾名思义就是一段一段的取.一开始一直往后取,符合条件,然后进行前面减少操作,不符合条件再往后取. 如此重复,得到最优的解.这道题目给出的页的标记好像有特别大的,直接用数组的话会爆炸. 试过用vector,但是对vector的操作太麻烦了.

题解报告: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(尺取法+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

题目传送门 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 尺取法/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 (尺取法)

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