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; }