原题题目
代码实现
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500010, M = 1000010;
int n, m;
int w[N], cnt[M];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
int res = 0, l, r;
for (int i = 1, j = 1, t = 0; i <= n; i ++ )
{
if (cnt[w[i]] == 0) t ++ ;
cnt[w[i]] ++ ;
while (t > m)
{
if (cnt[w[j]] == 1) t -- ;
cnt[w[j]] -- ;
j ++ ;
}
if (i - j + 1 > res)
{
res = i - j + 1;
l = j, r = i;
}
}
printf("%d %d\n", l, r);
return 0;
}