尺取法 POJ 3320 Jessica's Reading Problem

时间:2021-08-18 01:30:42

题目传送门

 /*
尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN]; int main(void) //POJ 3320 Jessica's Reading Problem
{
int n;
while (scanf ("%d", &n) == )
{
set<int> S;
for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]); S.insert (a[i]);
} map<int, int> cnt;
int tot = S.size (); int ans = n, num = ; int i = , j = ;
while ()
{
while (j <= n && num < tot) if (cnt[a[j++]]++ == ) num++;
if (num < tot) break;
ans = min (ans, j - i);
if (--cnt[a[i++]] == ) num--;
} printf ("%d\n", ans);
} return ;
}