Educational Codeforces Round 5 (D. Longest k-Good Segment)

D. Longest k-Good Segment
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The array a withn integers is given. Let's call the sequence of one or more consecutive elements inasegment. Also let's call the segmentk-good if it contains no more thank 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 usescanf/printf instead ofcin/cout in C++, prefer to useBufferedReader/PrintWriter instead ofScanner/System.out inJava.

Input

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

The second line contains n integersai (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 from1 ton from left to right.

Sample test(s)
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
题意:第一行给你两个数m,k;
第二行给你n个数;
要求你找一个连续区间,这个区间不同的数的个数不超过k而且这个区间长度最大,输出区间端点l,r(任意一组就行)
分析:two pointers解决,知道这个方法做这个题简直就是水题,直接看代码。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[500010];
int vis[1000010];
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)==2)
    {
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        memset(vis,0,sizeof(vis));
        int len=1,l=1,r=1,sum=1;///len记录最长的长度,用来更新答案而已,l和r分别指向区间两端,sum表示的是a[l]到a[r]中不同的数的个数
        int x=1,y=1;
        vis[a[1]]=1;///预处理第一个值
        while(r<=n)
        {
            r++;
            if(r>n) break;
            if(vis[a[r]]==0)///加入这个值之前没有这个值,那么不同的数多了1,sum++
            {
                sum++;
                vis[a[r]]++;
            }
            else vis[a[r]]++;
            while(sum>k)
            {
                vis[a[l]]--;
                if(!vis[a[l]]) sum--;///如果这个值-1后这个值就没有了,代表少了一个不同的数,sum--
                l++;
            }
            if(r-l+1>len)
            {
                len=r-l+1;
                x=l,y=r;
            }
        }
        printf("%d %d\n",x,y);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值