【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83155604

题干:

The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values.

Find any longest k-good segment.

As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input

The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k.

The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a.

Output

Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right.

Examples

Input

5 5
1 2 3 4 5

Output

1 5

Input

9 3
6 5 1 2 3 2 1 4 5

Output

3 7

Input

3 1
1 2 3

Output

1 1

题目大意:

在长度为n的序列 中找出有k个不同数字的最长连续子串,输出子串开始以及结束的位置(若有多个答案,输出任何 一个即可)

解题报告:

   尺取就好了。刚开始读错题了,读成了恰有k个不同数字了。。不过还好if(now==k)改成if(now<=k)即可。

AC代码:

//刚开始读错题了。。简单尺取、、
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> 
#define ll long long
using namespace std;
int a[(int)5e5 + 5],cnt[(int)1e6 + 5];
int n,k,ans,ansl,ansr;
int main()
{
	cin>>n>>k;
	for(int i = 1; i<=n; i++) scanf("%d",a+i);
	int l = 1,r = 1,now = 0;
	while(r <= n) {
		if(cnt[a[r]] == 0) now++;
		cnt[a[r]]++;
		while(now > k) {
			cnt[a[l]]--;
			if(cnt[a[l]] == 0) now--;
			l++;
		}
		if(now <= k) {
			if(r-l+1 > ans) {
				ansl = l;ansr = r;
				ans = r-l+1;
			}
		}
		r++;
	}
	printf("%d %d\n",ansl,ansr);
	return 0 ;
 } 

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83155604