POJ3320 Jessica's Reading Problem(尺取+map+set)
Posted xgboost
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3320 Jessica's Reading Problem(尺取+map+set)相关的知识,希望对你有一定的参考价值。
POJ3320 Jessica‘s Reading Problem
set用来统计所有不重复的知识点的数,map用来维护区间[s,t]上每个知识点出现的次数,此题很好的体现了map的灵活应用
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <vector> #include <map> #include <set> #include <string> #include <cmath> using namespace std; const int INF = 0x3f3f3f3f; typedef long long ll; const int MAX_P = 1000010; int P; int a[MAX_P]; int main() { scanf("%d", &P); for (int i = 0; i < P; ++i) { scanf("%d", &a[i]); } set <int> all; for (int i = 0; i < P; ++i) { all.insert(a[i]); } int n = all.size(); int s = 0, t = 0, num = 0; map<int, int> count; int res = P; for (;;) { while (t < P && num < n) { if (count[a[t]]== 0) { //出现了新的知识点 num++; } count[a[t]]++; t++; } if (num < n) break; res = min(res, t-s);//更新最小区间长度 count[a[s]]--; if (count[a[s]] == 0) //某个知识点出现次数为0 { num--; } s++; } printf("%d\n", res ); return 0; }
以上是关于POJ3320 Jessica's Reading Problem(尺取+map+set)的主要内容,如果未能解决你的问题,请参考以下文章
POJ - 3320 Jessica's Reading Problem
(尺取法)POJ 3320 Jessica's Reading Problem
POJ 3320Jessica's Reading Problem (尺取法)
poj3320 Jessica's Reading Problem