题目大意:给你一个长度为n的序列,问你里面不同的数不超过k个的最长序列是什么,给出左端点和右端点的值
题目思路:由于范围有点大,双指针扫一下就好
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int n,k,a[maxn],u[maxn],ans,l,r;
int main(){
scanf("%d%d",&n,&k);
for(int i = 0;i < n;i++)
scanf("%d",&a[i]);
ans = 1,l = r = 0;
for(int i = 0,j = 0,s = 0;i < n;i++){
while(j < n&& s + (u[a[j]] == 0) <= k){
if(u[a[j]] == 0) s++;
u[a[j]]++;
j++;
}
if(j-i > ans){
ans = j-i;
l = i;
r = j-1;
}
u[a[i]]--;
if(u[a[i]] == 0) s--;
}
printf("%d %d\n",l+1,r+1);
return 0;
}