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

P1638 逛画展(直尺法)

作者:互联网

这道题是直尺法的模板题:

  

#include<iostream>
using namespace std;
const int maxn = 1e6 + 5;
const int M = 2e3 + 5;
int n, m, a[maxn], vis[M];

int main(){
    cin >> n >> m;
    for (int i = 0; i < n; ++i)cin >> a[i];
    int st = 0, en = 0, num = 0, res = n, l = 1, r = n;
    while (1){
        while (en < n&&num < m){
            if (vis[a[en++]]++ == 0)num++;
        }
        if (num < m)break;
        if (res>en - st){ res = en - st; l = st + 1; r = en; }
        if (--vis[a[st++]] ==0 )num--;
    }
    cout << l << " " << r << endl;
}

 

标签:en,直尺,画展,vis,int,st,++,num,P1638
来源: https://www.cnblogs.com/ALINGMAOMAO/p/10460754.html