poj3320 尺取法~~

红太狼 2022-08-09 00:44 288阅读 0赞

Jessica’s Reading Problem














Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8810   Accepted: 2835

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

  1. 5
  2. 1 8 8 8 1

Sample Output

  1. 2

题意:有n个数,让你求最短的区间,这个区间必须包含n个数中所有不同的数。

分析:尺取大发好!区别不同的数用map。

  1. #include<bitset>
  2. #include<map>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<string>
  8. #include<algorithm>
  9. #include<cmath>
  10. #include<stack>
  11. #include<queue>
  12. #include<set>
  13. #define inf 0x3f3f3f3f
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15. using namespace std;
  16. typedef long long ll;
  17. typedef pair<int,int> pii;
  18. inline int in()
  19. {
  20. int res=0;char c;
  21. while((c=getchar())<'0' || c>'9');
  22. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  23. return res;
  24. }
  25. const int N=1000010;
  26. int a[N];
  27. map<int,int> mp;
  28. int main()
  29. {
  30. int n;
  31. while(~scanf("%d",&n))
  32. {
  33. mp.clear();
  34. for(int i=0;i<n;i++) a[i]=in(),mp[a[i]]++;
  35. int num=mp.size();
  36. int j=0,ans=inf,cnt=0;
  37. mp.clear();
  38. for(int i=0;i<n;i++)
  39. {
  40. for(;j<n && cnt<num;j++)
  41. {
  42. if(mp[a[j]]++ == 0){
  43. cnt++;
  44. }
  45. }
  46. if(cnt<num) break;
  47. ans=min(ans,j-i);
  48. if((--mp[a[i]])==0) cnt--;
  49. }
  50. printf("%d\n",ans);
  51. }
  52. return 0;
  53. }

发表评论

表情:
评论列表 (有 0 条评论,288人围观)

还没有评论,来说两句吧...

相关阅读

    相关 总结

    一般地,当我们要去枚举满足条件的区间权值的区间,可以用遍历双指针 l 和 r 的做法去枚举第一个满足条件的区间,然后去维护其他数据,这样的做法就是尺取法,它的复杂度为O(n)

    相关 算法--

    尺取法 尺取法通常是指对数组保存一对下标(起点,终点),然后根据实际情况交替推进两个端点直到得出答案的方法,这种操作很像是尺取虫爬行的方式故得名。 我们先来看看POJ的