题目链接:http://poj.org/problem?id=3320
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10787 | Accepted: 3606 |
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
Source
题目大意:给你一本书,里面有很多知识点,并且很多都是重复的,求最少要读多少页,使得学完整本书
解析: 运用HASH思想,把每个知识点的个数 和 总知识点个数 统计下,然后运用尺取法一步步让它接近最小即可
代码如下:
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1000009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
map<int, int> Hash, mp;
int num[N];
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
if(!mp.count(num[i])) mp[num[i]] = 0;
mp[num[i]]++;
}
int s = mp.size(), l, r;
l = r = 1;
int ans = inf;
while(1)
{
while(r <= n && s > Hash.size())
Hash[num[r++]]++;
if(s > Hash.size()) break;
ans = min(ans, r - l);
Hash[num[l++]]--;
if(Hash[num[l - 1]] <= 0) Hash.erase(num[l - 1]);
}
cout << ans << endl;
return 0;
}