【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)
题干:
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 ;}
【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)相关推荐
- CodeForces - 616D Longest k-Good Segment
CodeForces - 616D Longest k-Good Segment 题意: 有包含n个数的序列a,求能找到最长的区间包含不超过k个不同的元素. 题解: 尺取法,先固定L,然后移动R,R每 ...
- CodeForces - 1358D The Best Vacation(前缀和+尺取)
题目链接:点击查看 题目大意:给出 n 个数组成的数列,每个元素都可以展开为 1 , 2 , 3 .... a[ n ] ,现在将数列首尾相接,要求选取一段长度为 x 的连续数列,使得元素和最大 题目 ...
- 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 ...
- 【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)
题干: You are given an array a with n elements. Each element of a is either 0 or 1. Let's denote the l ...
- 【尺取或dp】codeforces C. An impassioned circulation of affection
http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...
- CodeForces - 1198A MP3(尺取)
题目链接:点击查看 题目大意:给出n个数字,表示不同的数据,现在我们需要对数据进行压缩,压缩的规则是: 现在给出一个I,表示内存大小为,n个数字中,相同的数据可以占用同一片内存,不同的数据必须占用不同 ...
- Codeforces Round #736 (Div. 2) D. Integers Have Friends ST表gcd + 尺取
传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,求一个最长的子序列[l,r][l,r][l,r]满足aimodm=ai+1modm=...=armodma_i\bmod m=a_{i+1 ...
- 【CodeForces - 514D】R2D2 and Droid Army(二分+滑动窗口ST表,或 尺取+单调队列或STLmultiset)
题干: An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., a ...
- 【51Nod - 1001 】 数组中和等于K的数对 (排序+ 尺取)
题干: 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9 ...
最新文章
- 使用.NET自带的类实现DataGrid报表的打印。
- Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例
- java hashset应用_三.java集合的应用
- 配置IIS Silverlight运行环境
- 总结一下嵌入式OLED显示屏显示中文汉字的办法
- 深度学习核心技术精讲100篇(四十六)-情感分析算法在阿里小蜜的应用实践
- Android Activity动画属性简介
- OpenCV AKAZE本地特征匹配
- linux 环境下安装和配置mysql数据库以及远程登录
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
- Python小白的数学建模课-02.数据导入
- python并发编程5-线程
- hibernate分页中跳转到第几页的功能
- linux cat 文件合并,linux cat两个文件是肿么合并的
- 【笔记】移植C的GNU Go到网页版
- 他捧红了王菲、张学友等近百个巨星,却甘心成为最普通的学佛人…
- Unity中迭代器和协程的部分概念
- byref与byval的区别
- 职场神攻略:5分钟自我介绍法 快准狠!
- 读书笔记 - 《天局》
热门文章
- [hackinglab][CTF][综合关][2020] hackinglab 综合关 writeup
- [剑指offer]面试题第[41]题[Leetcode][第235题][JAVA][数据流中的中位数][优先队列][堆]
- java 添加注解_你知道Java中的package-info的作用吗?
- unityscrollview生成大量_superscrollviewforUGUI的一些使用心得
- c语言字符串未初始化strcat,C语言中字符串常用函数strcat与strcpy的用法介绍
- esmini接入外部ego车控制
- 解密ControlRotation与ActorRotation
- 网络编程模型综述 之 多线程模型
- c位边上还有什么位_【一点资讯】那些有趣而搞笑的GIF动画:为让出c位,站边上拍照还是谦虚礼貌的 www.yidianzixun.com...
- 小数点化分数的过程_分数和小数的互化