poj 3320 Jessica'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 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

今天开始学算法系列 -- 尺取法 2015-02-28 09:54:49

分类: C/C++

转载一段讲解

我们先来介绍一下尺取法。尺取法,顾名思义,像尺子一样,一块一块的截取。是不是解释的有点让人纳闷~。。没关系,下面我们通过这个题目来体会尺取法的魅力。

题目翻译:

  给定长度为n的数列整数a0,a1,a2,a3 ..... an-1以及整数S。求出综合不小于S的连续子序列的长度的最小值。如果解不存在,则输出0。

  限制条件:

    10<n<10^5 <="" p="">

    0<ai<10^4 <="" p="">

    S<10^8

这里我们拿第一组测试数据举例子,即 n=10, S = 15, a = {5,1,3,5,10,7,4,9,2,8}

   这幅图便是尺取法怎么“取”的过程了。

  整个过程分为4布:

    1.初始化左右端点

    2.不断扩大右端点,直到满足条件

    3.如果第二步中无法满足条件,则终止,否则更新结果

    4.将左端点扩大1,然后回到第二步

用尺取法来优化,使复杂度降为了O(n)。

最后,再给一个尺取法的定义以便更好理解:返回的推进区间开头和结尾,求满足条件的最小区间的方法称为尺取法。

以上为网上关于尺取法的原理介绍,还是比较好理解的,邢如蚯蚓的爬动。

  然后这道题可以先用set ,统计出不同的知识点有多少个,总是记为total

  然后根据一段区间内的知识点数目sum是否达到total来移动两个pointer head 和tail

  因为知识点的标号比较大,数组下表存不下,所以开个map来统计相应知识点出现的数量...

  然后还有个...

  误以为if (cnt[a[tail++]]++==0) sum++;和

  if (cnt[a[tail]]==0)

  {

    sum++;

    cnt[a[taill]]++;

    tail++;

  }

  是等价的...

幸好没去队群里问...不然又要被嘲讽一番了..

区别在于.前者无论cnt[a[tail]]是否为0,tail和cnt都会++;

而后者..只有在cnt[a[tail]]为0时,tail和cnt才会++

 1 /*************************************************************************
 2     > File Name: code/poj/3320.cpp
 3     > Author: 111qqz
 4     > Email: [email protected]
 5     > Created Time: 2015年09月24日 星期四 19时33分20秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define y1 hust111qqz
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 #define lr dying111qqz
26 using namespace std;
27 #define For(i, n) for (int i=0;i<int(n);++i)
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 const int N = 1E6+7;
32 int a[N];
33 int p;
34 void solve ()
35 {
36     set<int>se;
37     for ( int i = 1 ; i <= p ; i++)
38     {
39     scanf("%d",&a[i]);
40     se.insert(a[i]);
41     }
42     int total = se.size();
43     map<int,int> cnt;
44     int ans = p ;
45     int head = 1 ;
46     int tail = 1 ;
47     int sum = 0 ;
48     while (1)
49     {
50     while (tail<=p&&sum<total)
51     {
52         // cout<<"ajhhh"<<endl;
53      //  if (cnt[a[tail++]]++ ==0) sum++;
54          if (cnt[a[tail]]==0)
55         {
56
57         sum++;
58         }
59          cnt[a[tail]]++;
60              tail++;
61
62
63 //         cout<<"total"<<total<<endl;
64     }
65     if (sum<total) break;
66     ans = min (ans ,tail - head);
67     if (--cnt[a[head++]]==0) sum--;
68
69     }
70     printf("%d\n",ans);
71 }
72 int main()
73 {
74   #ifndef  ONLINE_JUDGE
75    freopen("in.txt","r",stdin);
76   #endif
77    while (scanf("%d",&p)!=EOF)
78     {
79     solve();
80     }
81
82
83  #ifndef ONLINE_JUDGE
84   fclose(stdin);
85   #endif
86     return 0;
87 }

poj 3320 Jessica's Reading Problem (尺取法)

时间: 2024-10-07 06:14:01

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

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

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