其他分享
首页 > 其他分享> > P1638 逛画展(尺取法)

P1638 逛画展(尺取法)

作者:互联网

https://www.luogu.com.cn/problem/P1638

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6 + 10;
int n,m;
int a[maxn],b[maxn];
int l = 1, r= 1;
int ans = maxn,k = 1;
int ll,rr;
signed main(){
    //freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    b[a[1]] = 1;
    while(l <= r && r <= n){
        if(k == m){
            if(ans > r - l + 1){
                ans =  r - l + 1;
                ll = l,rr = r;
            }
           b[a[l]]--;
            if(!b[a[l]])
                k--;
            l++;
        }else{
            r++;
            b[a[r]]++;
            if(b[a[r]] == 1)
                k++;
        }
    }
    cout << ll << " " << rr;
    return 0;
}

 

标签:画展,int,ll,long,++,maxn,取法,ans,P1638
来源: https://www.cnblogs.com/xcfxcf/p/12618137.html