P1638 逛画展

题解:维护一个涵盖所有画师的滑动窗口,尽量让窗口内每种画的数量保持在洛谷P1638 逛画展(贪心)_洛谷P1638,记录长度最短的窗口端点。

代码

#include<bits/stdc++.h>

using namespace std;
int n,m,d[1000010],now[2001];

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
cin>>n>>m;
for(int i = 1; i <= n; ++i){
cin>>d[i];
}

int front = 1, rear = 1, ans_X = 1, ans_Y = 1e7, cnt = 0;
while(front <= rear){
if(now[d[rear]] == 0) cnt++;
if(rear<=n)
now[d[rear++]]++;
for( ; now[d[front]] > 1; front++)
now[d[front]]--;
if(cnt >= m){
if(rear - front < ans_Y - ans_X)
ans_X = front, ans_Y = rear;
}
if(rear == n + 1) break;
}
cout<<ans_X<<' '<<ans_Y-1<<endl;
return 0;
}