[洛谷P1638]逛画展

[洛谷P1638]逛画展

题目大意:

\(n(n\le10^6)\)个格子,每个格子有一种颜色。颜色种数为\(m(m\le2000)\)。求包含所有颜色的最小区间。

思路:

尺取法裸题。

思路:

#include<cstdio>
#include<cctype>
#include<climits>
inline int getint() {
    register char ch;
    while(!isdigit(ch=getchar()));
    register int x=ch^'0';
    while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    return x;
}
const int N=1e6+1,M=2001;
int a[N],cnt[M],tot;
int main() {
    const int n=getint(),m=getint();
    for(register int i=1;i<=n;i++) {
        a[i]=getint();
    }
    int min=INT_MAX,b,e;
    for(register int l=1,r=0;l<=n;l++) {
        for(;tot<m&&r<n;) {
            if(!cnt[a[++r]]++) tot++;
        }
        if(tot==m) {
            if(r-l+1<min) {
                min=r-l+1;
                b=INT_MAX;
            }
            if(r-l+1==min&&l<b) {
                b=l,e=r;
            }
        }
        if(!--cnt[a[l]]) tot--;
    }
    printf("%d %d\n",b,e);
    return 0;
}

转载于:https://www.cnblogs.com/skylee03/p/9870183.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值