Codeforces Round #138 (Div. 2) B. Array

37 篇文章 0 订阅

B. Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 integersa1, 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.

Sample test(s)
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.

题意: 从一个序列中取出一个子序列,里面刚好有k个不同的数,而且,其任意一个子序列都不能有k个不同的数。那么我的想法是开头和结尾的数在整个序列中必须只存在一个。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
  int n,k,i,j,s=0,e=0;
  int a[100005],b[100005];
 while(~scanf("%d%d",&n,&k))
   {
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
     for(i=0;i<n;i++)
     {
         scanf("%d",&a[i]);
     }
    int sum=0;
      for(i=0;i<n;i++)
      {
          if(b[a[i]]==0) sum++;
          b[a[i]]++;
        if(sum==k) { e=i; break;}
      }
      s=0;
    if(sum==k)
      {
        while(b[a[s]]>1)  {b[a[s]]--;s++;}
          printf("%d %d\n",s+1,e+1);
      }
    else
     printf("-1 -1\n");


   }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值