Longest k-Good Segment CodeForces - 616D(尺取法)
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
思路:做了这几个尺取法的题目,总结出这样的一种套路,对于这种类型的尺取来说,这也可以当做一个模板了(和我上一篇博客差别不大)。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=5e5+100;
int a[maxx];
int n,k;int main()
{scanf("%d%d",&n,&k);for(int i=1;i<=n;i++) scanf("%d",&a[i]);map<int,int> mp;mp.clear();int sum=0,l=1,r=1,_max=0;int L=1,R=1;while(r<=n){while(sum<=k&&r<=n){if(mp[a[r]]==0) sum++;mp[a[r]]++;r++;}if(sum>k) r--,mp[a[r]]--,sum--;if(_max<r-l){_max=r-l;L=l,R=r-1;}while(sum>=k&&l<=n){mp[a[l]]--;if(mp[a[l]]==0) sum--;l++;}}printf("%d %d\n",L,R);return 0;
}
努力加油a啊,(o)/~
Longest k-Good Segment CodeForces - 616D(尺取法)相关推荐
- Codeforces 1167E 尺取法
题意:给你一个长度为n的数组,以及数组中的数的取值范围1 - m,问有多少个区间[l, r],使得删除了数组中数值为[l, r]的数之后,数组是非递减的. 思路:我们记录一下每一个数出现的最左端和最右 ...
- CodeForces - 616D Longest k-Good Segment
CodeForces - 616D Longest k-Good Segment 题意: 有包含n个数的序列a,求能找到最长的区间包含不超过k个不同的元素. 题解: 尺取法,先固定L,然后移动R,R每 ...
- Vasya and String CodeForces - 676C(尺取法)
High school student Vasya got a string of length n as a birthday present. This string consists of le ...
- Codeforces 1548B Integers Have Friends 尺取法 Hdu 7073 Integers Have Friends 2.0 力能扛鼎随机算法
文章目录 题意 题解 CF1548B 题解 Hdu 7073 CF1548B HDU7073 题意 定义数的好友组为一个集合SSS,取正整数m>1,∀x∈s,xmodmm>1,\foral ...
- Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)
题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...
- QDUOJ 河老师的新年礼物(尺取法)
河老师的新年礼物 发布时间: 2017年1月1日 15:11 最后更新: 2017年1月1日 15:13 时间限制: 1000ms 内存限制: 256M 描述 河老师的新年礼物是一个长度为 ...
- CF660C Hard Process(尺取法)
整理的算法模板合集: ACM模板 尺取法 题目中要我们求最多改变k次后连续的1的最长长度.那么转换一下,不就是求至多包含k个0的子串的最长长度吗? 直接套用尺取法的思想,维护两端点,一旦0的个数超过k ...
- 【常用技巧精选】尺取法
整理的算法模板合集: ACM模板 目录 1.反向扫描 1 找指定和的整数对 A.UVA1121 Subsequence B.POJ 3320 Jessica's Reading Problem C.l ...
- ACM—各种模拟 总结(字符串,尺取法,数学问题)习题汇总
目录 一.字符串模拟 二. 高精度计算 1. 回文数(高精度,进制转换) 三.数学问题模拟 四.尺取法(双指针法) 1.都说小镇的切糕贵 (尺取法,字符串) umi和弓道 五.奇怪的模拟 x的位数=l ...
最新文章
- 动态排序JavaBean
- 架构之:微服务和单体服务之争
- 用DTS将 SQLServer 导出到 Oracle
- MySQL数据库加密和解密~认证登陆密码(mysql.user)和MySQL不区分大小写
- JAVA注解行_java注解
- python ks值计算_风控模型中的K-S理解以及python实现
- VPS搭建zotero自动同步的webdav服务
- 3000个最常用的英语单词
- java update是什么_javaupdate是什么_tbupdate
- 致敬赵雷:基于TensorFlow让机器生成赵雷曲风的歌词
- Hexo+腾讯云+Icarus主题 搭建自定义个人博客
- 小小屋影视全网搜索在线播放工具
- python入侵手机_Python-Iocextract:高级入侵威胁标识符IoC提取工具
- 快手Java开发二面面经分享
- c语言检测邮箱地址,c语言实现邮箱地址验证
- 2022广东最新八大员之(安全员)模拟试题题库及答案
- Matplotlib使用Latex中文
- GAN生成对抗网络论文翻译(一)
- Virtualbox 设置共享文件夹
- 电源硬件设计----降压-升压(Buck-Boost)变换器基础
热门文章
- IOS开发地理编码与反向编码
- win10 hyper ubuntu18 共享文件夹
- pip /usr/bin/pip: No such file or directory
- JTAG、JLINK、ULINK、ST-LINK的联系和区别
- extjs获取元素name属性值_【ExtJS】各种获取元素组件方法
- sql数据类型转换oracle,ORACLE SQL数据类型转换
- Android开发之设置listview分割线的颜色
- 使用Tomcat-redis-session-manager来实现Tomcat集群部署中的Session共享
- 513. Find Bottom Left Tree Value
- 操作系统难点解析之进程和线程