双指针法 Codeforces Round #138 (Div. 2) B题 224B

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值