Jessica's Reading Problem poj3320(尺取法 +测试数据)

Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11226   Accepted: 3777

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

题目分析:

本来我的想法是声明两个指针指向数列的头部和尾部,当然之前整个数列都存在map当中,然后前面指针往后移,移动的过程中,如果出现map[数字]-1==0的情况就说明这一个元素已经只有一个,则前面的指针就不能移动了,按同样的办法移动后面的指针。本想这样是可以过得,不料一直WA。。。。。。

原因是这样移动是一个一个的移除数列中的元素,但是这样移除的顺序会有响最终结果。

后面看的尺取法:用set把数列中元素种类求出来(set是不可重复集合),然后两指针fir,sec同时指向头部,然后sec往后移动,一直到fir和sec之间的元素个数已经有总个数那么多了。然后用res存储此时的sec-fir。移动fir,直到fir和sec之间的元素个数小于总个数,又再次移动sec,重复上述步骤,每一次当fir和sec之间的元素个数等于总个数时,更新res。最后得到的res就是结果。

代码如下:

#include
#include
#include
#include
#include
using namespace std;
const int maxx=1000000+50;
mapma;
set se;
int cun[maxx];
int main(){
    int mysize=0;
int p,a;
//    freopen("data.in","r",stdin);
//    freopen("data.out","w",stdout);
    while(scanf("%d",&p)!=EOF){
            se.clear();
            ma.clear();
for(int i=0;i

附赠测试数据:

6
1 1 1 1 1 1
5
1 8 8 8 1
6
1 2 3 4 5 6
5
1 2 3 4 4
8
1 2 3 3 3 4 5 6
10
1 2 2 2 4 2 5 2 2 2
18
1 2 3 3 3 3 2 1 1 1 1 2 2 2 2 2 3 3
5
1 2 2 3 1
5
1 2 2 2 1
5
1 2 2 2 2
6
1 2 3 2 3 1
9
1 2 3 4 5 4 3 2 1
10
1 2 7 9 10 7 10 3 2 1
11
1 1 2 6 8 5 6 6 6 6 


答案为:

case #1: 1
 case #2: 2
 case #3: 6
 case #4: 4
 case #5: 8
 case #6: 7
 case #7: 3
 case #8: 3
 case #9: 2
 case #10: 2
 case #11: 3
 case #12: 5
 case #13: 7
 case #14: 5


你可能感兴趣的:(特殊技巧)