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

poj 3320

时间:2017-05-10 01:03:11      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:printf   can   include   set   str   algo   min   i++   max   

尺取法,这里的边界是知识点总数量,看代码就明白

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const int maxn=1e7+10;
int n;
int a[maxn];
set<int> aa;
int chiqu()
{
    int tt=aa.size();
    map<int,int> bb;//知识点以及他们对应的数量
    int num=0,t=0,s=0;//num是知识点数量
    int ans=n+1;
    while(1)
    {
        while(t<n&&num<tt)
        {
            if(bb[a[t++]]++==0)
                num++;
        }
        if(num<tt) break;
        ans=min(ans,t-s);
        if(--bb[a[s++]]==0)
            num--;//如果是只出现过一次的知识点,那么总知识点减一,否则不减,但s依旧会++
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        aa.insert(a[i]);
    }
    printf("%d\n",chiqu());
    return 0;
}

 

poj 3320

标签:printf   can   include   set   str   algo   min   i++   max   

原文地址:http://www.cnblogs.com/Wangwanxiang/p/6833353.html

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