poj3320 Jessica's Reading Problem

思路:

尺取。

实现:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <map>
 4 using namespace std;
 5 const int INF = 0x3f3f3f3f;
 6 int a[1000005];
 7 map<int, int> mp;
 8 int main()
 9 {
10     int n;
11     scanf("%d", &n);
12     for (int i = 0; i < n; i++) { scanf("%d", &a[i]); mp[a[i]]++; }
13     int i = 0, j = 0, ans = INF, tot = mp.size();
14     mp.clear();
15     while (true)
16     {
17         while (j < n && (int)mp.size() < tot) { mp[a[j]]++; j++; }
18         if (j == n && i == j) break;
19         if ((int)mp.size() == tot) ans = min(ans, j - i);
20         mp[a[i]]--; if (!mp[a[i]]) mp.erase(a[i]);
21         i++;
22     }
23     cout << ans << endl;
24     return 0;
25 }
原文地址:https://www.cnblogs.com/wangyiming/p/7620254.html