• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
村雨sup
自己选的路,跪着也要走完 XD
博客园    首页    新随笔    联系   管理    订阅  订阅
尺取法拓展——POJ3320
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef long long  ll;
const int INF = 0x3f3f3f3f;
const int moder = 1e9 + 7;
const int maxn = 1000000;

int a[maxn];

int main()
{
    int P;
    scanf("%d",&P);
    for(int i=0;i < P;i++)
        scanf("%d",&a[i]);

    set<int>st;
    for(int i=0;i < P;i++)
        st.insert(a[i]);

    int n = st.size();

    int s=0,t=0,num=0;
    map<int,int>cnt;
    int res = P+1;
    for(;;)
    {
        while(t < P && num < n)
        {
            if(cnt[a[t]] == 0)
            {
                num++;
            }
            cnt[a[t]]++;
            t++;
        }
        if(num < n) break;
        res = min(res,t-s);
        if(--cnt[a[s++]] == 0)
        {
            num--;
        }
    }
    printf("%d\n",res);
    return 0;
}

/*5
1 8 8 8 1 */

map很好用,记住。

posted on 2018-02-14 00:38  村雨sup  阅读(108)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3