[题解]P1638 逛画展

滑动窗口法,暂时没有思路,先存一篇题解,有时间回来看

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int n,m;
const int MAX=2e5+10;
vector<int>v(MAX),f(MAX);//f:当前窗口中该画师作品数
int ansl,ansr;
void solve(){
   int l=0,r=-1,i=0,cnt=0;//i:当前工作指针
   while(cnt!=n){
		if(!f[v[i]]) cnt++;
		f[v[i]]++;
		r++,i++;
   }
   while(f[v[l]]>1) f[v[l++]]--;
   ansl=l,ansr=r;
   while(i<m){
	f[v[i]]++;
	r++,i++;
	while(f[v[l]]>1) f[v[l++]]--;
	if(ansr-ansl>r-l){
		ansr=r,ansl=l;
	}
   }
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>m>>n;
    v.resize(m),f.resize(n+1);
    for(int i=0;i<m;i++) cin>>v[i];
    solve();
    cout<<ansl+1<<' '<<ansr+1;
    return 0;
}
posted @ 2024-07-10 19:06  椰萝Yerosius  阅读(9)  评论(0)    收藏  举报  来源