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.
Examples
input
4 2
1 2 2 3
output
1 2
input
8 3
1 1 2 2 3 3 4 5
output
2 5
input
7 4
4 7 7 4 7 4 7
output
-1 -1
Note
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.
In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.
In the third sample there is no segment with four distinct numbers.
模拟过程 但是这道题比较吃题意 题意理解不好很容易wa
大致思想就是从首先从左到右搜到有效的界限区间
固定右节点然后
从左到右搜索左节点:
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int a[100001];
bool vis[100001]; //标记记录量
int main()
{
int n,k;
int l=0,r=0;
int sum=0;
cin>>n>>k;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(vis[a[i]]==0) //遍历记录不重复数据的个数
{
sum++;
vis[a[i]]=1;
}
}
if(sum<k) //如果数据中包含不同的数据小于要求 k个直接舍去结束程序
{
cout<<"-1 -1"<<endl;
return 0;
}
sum=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) //先从左到右搜索满足的界限
{
if(vis[a[i]]==0)
{
sum++;
vis[a[i]]=1;
if(sum==k) //遍历搜到右节点并记录节点位置
{
r=i;
break;
}
}
}
sum=0;
memset(vis,0,sizeof(vis));
for(int i=r;i>=1;i--) //再从右向左在满足的界限内搜索左节点
{
if(vis[a[i]]==0)
{
sum++;
vis[a[i]]=1;
if(sum==k) //搜索左节点并记录
{
l=i;
break;
}
}
}
cout<<l<<" "<<r<<endl;
return 0;
}