Array
You’ve got an array a, consisting of n integers: a1, a2, …, an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al, al + 1, …, ar there are exactly k distinct numbers.
Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn’t have to be minimal in length among all segments, satisfying the given property.
Input
The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, …, an — elements of the array a (1 ≤ ai ≤ 105).
Output
Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print “-1 -1” without the quotes. If there are multiple correct answers, print any of them.
题目大意为给你一个数组,让你找一个l,r区间,这个区间有k个不相同的值,l,r不能有子区间也有k个不同的值;
跟这道题一模一样:**博客链接**
代码:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000000;
const LL mod=1e9+7;
int n,k,a[N],vis[N];
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
int sum=0,l=1,L=-1,R=-1,len=2e9;
for(int r=1;r<=n;r++){
if(!vis[a[r]]) sum++;
vis[a[r]]++;
if(sum==k){
while(l<=r){
if(vis[a[l]]>=2) vis[a[l]]--,l++;
else break;
}
if(len>r-l+1){
len=r-l+1;
L=l,R=r;
}
}
}
cout<<L<<" "<<R<<endl;
return 0;
}