尺取法拓展——POJ3320

本文介绍了一个使用C++编程语言解决特定算法问题的例子。该程序利用了set和map容器来实现对输入数据的有效处理,具体包括读取整数数组、去除重复元素并统计不重复元素的数量等操作。通过实例演示了如何在C++中高效地应用这些STL容器。
#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很好用,记住。

转载于:https://www.cnblogs.com/cunyusup/p/8447788.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值