Codeforces-224B Codeforces Round #138 (Div. 2) B題

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個不同的值;

這是一道雙指針經典問題的弱化版;

**博客鏈接**

直接記錄每個值出現的次數,當不同的值出現了k次後,直接記錄R,跳出循環;

然後從 L 開始遍歷,直到找到一個數的出現次數爲 1 ;

代碼:

#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=100100;
const int M=1000100;
const ll mod=1e9+7;
int a[N];
int vis[N];
int main(){
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    if(k==1){
    	cout<<1<<" "<<1<<endl;
    	return 0;
	}
	int l=1,r=1,cnt=1,ok=1;
    vis[a[1]]++;
    for(int i=2;i<=n;i++){
    	if(!vis[a[i]]) cnt++;
    	vis[a[i]]++;
		if(cnt>=k){
			r=i;
			ok=0;
			break;
		}
	}
	if(ok){
		cout<<-1<<" "<<-1<<endl;
		return 0;
	}
	for(int i=1;i<=r;i++){
		if(vis[a[i]]>1) l++,vis[a[i]]--;
		else break;
	}
	cout<<l<<" "<<r<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章