D. Longest k-Good Segment

题目连接:

http://www.codeforces.com/contest/616/problem/D

Description

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.

Sample Input

5 5

1 2 3 4 5

Sample Output

1 5

Hint

题意

给你n个数,你需要找到一个最长的区间,使得这个区间里面不同的数小于等于k个

题解:

尺取法扫一遍就好了

代码

#include<bits/stdc++.h>
using namespace std; #define maxn 1000005
int a[maxn];
int vis[maxn];
int main()
{
int n,k;scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int al=0,ar=0,ans=0,now=0,l=1;
for(int i=1;i<=n;i++)
{
vis[a[i]]++;
if(vis[a[i]]==1)now++;
while(now>k)
{
vis[a[l]]--;
if(vis[a[l]]==0)
now--;
l++;
}
if(i-l+1>=ar-al+1)
ar=i,al=l;
}
cout<<al<<" "<<ar<<endl;
}

Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法的更多相关文章

  1. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  2. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  3. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  4. A - Longest k-Good Segment (尺取法)

    题目链接: https://cn.vjudge.net/contest/249801#problem/A 解题思路:尺取法,每次让尺子中包含k种不同的数,然后求最大. 代码: #include< ...

  5. Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph

    E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...

  6. Codeforces Educational Codeforces Round 15 D. Road to Post Office

    D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学

    E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...

  8. Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...

  9. Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心

    D. Gadgets for dollars and pounds 题目连接: http://www.codeforces.com/contest/609/problem/C Description ...

随机推荐

  1. dzzoffice注册开启

    dzzoffice默认安装注册选线是关闭的,需要在“系统设置”里打开. 设置方法 开始菜单=>系统设置=>注册与访问=> 将允许用户注册选勾,选上. 然后提交保存.

  2. ISO-8859-1

    ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x80-0x9F之间是控制字符,0xA0-0xFF之间是文字符号 ...

  3. php连接数据库

    <?php header('Content-Type:text/html; charset=utf-8'); define('DB_HOST', 'localhost'); define('DB ...

  4. ShellExecute的各种用法

    一.利用系统默认的邮件收发器发送电子邮件 Uses ..., ShellAPI; Var lpHwnd: HWND; lpOperation, lpFile, lpParameters, lpDire ...

  5. bluebird-Core API(一)

    new Promise new Promise(function(function resolve, function reject) resolver) -> Promise 创建一个新的Pr ...

  6. Jquery 遍历表单 AJAX提交

    function test(){ var arrayObj = new Array(); $("#contentTable tbody tr").each(function(){ ...

  7. Azure Cloud中的Log4Net设置

    这里的Cloud包含Worker Role和Web Role,Role是运行在云主机中的,这里的主机和VM有所不同,Windows Azure Role Architecture.我们并没有和本地服务 ...

  8. Who is the best at Dataset X?

    推荐一个关于分类.目标检测.姿态估计的数据集收藏的网页. Did you ever want to quickly learn?which paper provides the best result ...

  9. 使用DNSPod来处理网站的均衡负载(转)

    add by zhj:配置倒是蛮简单的,其实就是把域名与多个IP进行关联,在数据库中实现这个应该也是蛮简单的. 原文:http://kb.cnblogs.com/page/75571/ 首先介绍下DN ...

  10. RFID第二次作业

    1.简述智能卡的发展,以及射频电子标签在其中所处的位置. 智能卡(Smart Card)又称为集成电路卡(IC卡),内部带有微处理器和存储单元等部件. 射频电子标签是一种非接触式的IC卡,是后期发展起 ...