poj3320 Jessica's Reading Problem(尺取思路+STL)

https://vjudge.net/problem/POJ-3320

尺取法,要想好组织方式。

又被卡了cin。。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
#include<set> 
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
set<int> st;
map<int, int> mp;
int a[100010];
int main()
{
    
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        st.insert(a[i]);
        mp[a[i]] = 0;
    }
    int m = st.size();
    int s=0, t=0, num=0, ans = INF;
    while(1){
        while(t < n&&num < m){//判断种类是否都涵盖
            if(mp[a[t++]]++ == 0){//如果是该类知识点的第一个
                num++;//种类++
            }
        }
        if(num < m) break;
        ans = min(ans, t-s);
        if(--mp[a[s++]] == 0){//后判断a[s]的类的个数--后是否为0
            num--;//种类--
        }
    }
    printf("%d\n", ans); 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Surprisezang/p/9019287.html