【CodeForces 616D】Longest k-Good Segment
题意
n个数里,找到最长的一个连续序列使里面最多k个不同的数。
分析
尺取法,每次R++,如果第R个数未出现过,那么不同的数+1,然后这个数的出现次数+1,如果不同的数大于k了,那就要去掉第L个数,直到不同的数为k,然后更新答案。
代码
#include<cstdio>
#define ll long long
#define N 500005 int n,k;
int a[],num[];
int l,r;
int s,ansl,ansr; int main()
{
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
ansl=;
ansr=k;
l=;
r=;
while(r<n)
{
r++;
if(num[a[r]]==)s++;
num[a[r]]++;
while(s>k)
{
num[a[l]]--;
if(num[a[l]]==)s--;
l++;
}
if(r-l>ansr-ansl)
{
ansr=r;
ansl=l;
}
}
printf("%d %d",ansl,ansr);
return ;
}
【CodeForces 616D】Longest k-Good Segment的更多相关文章
- 【Codeforces 632D】 Longest Subsequence
[题目链接] 点击打开链接 [算法] 设取的所有数都是k的约数,则这些数的lcm必然不大于k. 对于[1, m]中的每个数,统计a中有多少个数是它的约数即可. [代码] #include<bit ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 一本通1649【例 2】2^k 进制数
1649:[例 2]2^k 进制数 时间限制: 1000 ms 内存限制: 524288 KB [题目描述] 原题来自:NOIP 2006 提高组 设 r 是个 2k 进制数,并满足以 ...
- 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 242E】XOR on Segment
[原题题面]传送门 [题面翻译]传送门 [解题思路] 操作涉及到区间求和和区间异或,考虑到异或操作,我们对每个数二进制分解. 把每一位单独提出来做,异或要么取反要么变为不变,对于每一位建一颗线段树,那 ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755D】PolandBall and Polygon
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【23.26%】【codeforces 747D】Winter Is Coming
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【20.51%】【codeforces 610D】Vika and Segments
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- java8-1 final
1.final可以修饰类,方法,变量 特点: final可以修饰类,该类不能被继承. final可以修饰方法,该方法不能被重写.(覆盖,复写) final可以修饰变量,该变量不能被重新赋值.因为这个变 ...
- <<Effective Java>>之善用组合而不是继承
使用JAVA这门OO语言,第一要义就是,如果类不是专门设计来用于被继承的就尽量不要使用继承而应该使用组合 从上图2看,我们的类B复写了类A的add喝addALL方法,目的是每次调用的时候,我们就能统计 ...
- SVN和git的使用(附github的简单玩法)
今天简单的总结了下SVN和git的使用,也尝试了下github,应该好好提高下自己的英文水平了,梦想有一天不再使用任何翻译软件. [svn]:集中式的代码管理工具(版本控制工具--版本记录) 1> ...
- UICollectionView移动
collectionView在iOS9中发布了一个可以移动cell的新特性,实现如下: 1.创建collectionView并设置代理 - (UICollectionView *)collection ...
- KindEditor
1.官网 www.kindsoft.net 2.MVC下空置处理 例: 页面使用 @model XXModel....@Html.EditorFor(model => model.Content ...
- WebApi 消息拦截
最近公司要求对WebApi 实现服务端信息的监控(服务端信息拦截),由于本人之前没有做过这方便的相关项目所以在做的过程中也是困难重重,探索的过程也是非常痛苦的,好歹最终也算实现了这个功能.所以将这个分 ...
- ruby on rails 里使用SideKiq 做后台任务
环境:ubuntu14.4,ruby2.1.5, rails4.2 一.新一个rais项目:rails new active_job --skip-bundle 进入项目文件夹: cd a ...
- android 中打 Log 的一些技巧
在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋 ...
- 在C#代码中应用Log4Net 中配置文件的解释
一个完整的配置文件的例子如下所示,这个是”在C#代码中应用Log4Net(二)”中使用的配置文件. <log4net> <!-- 错误日志类--> <logger nam ...
- python 反模式
不使用 pythonic 的循环: l = [1,2,3] #Bad for i in range(0,len(list)): le = l[i] print(i,le) #Good for i,le ...