码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3320 Jessica's Reading Problem(尺取+map+set)

时间:2016-04-05 00:23:11      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

  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)

标签:

原文地址:http://www.cnblogs.com/akrusher/p/5353213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!