用队列维护一下即可

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
int n,k;
int a[maxn];
int tot[+];
struct Node
{
int id;
int val;
} node[maxn];
queue<Node>Q; int main()
{
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
node[i].id=i;
node[i].val=a[i];
} memset(tot,,sizeof tot);
int ans=,ansl,ansr;
int now=; for(int i=; i<=n; i++)
{
Q.push(node[i]);
if(tot[node[i].val]==) now++;
tot[node[i].val]++;
if(now<=k)
{
Node head=Q.front();
if(node[i].id-head.id+>ans)
{
ans=node[i].id-head.id+;
ansl=head.id;
ansr=node[i].id;
}
}
else if(now>k)
{
while()
{
Node head=Q.front();
if(tot[head.val]==) now--;
tot[head.val]--;
Q.pop();
if(now==k) break;
} Node head=Q.front();
if(node[i].id-head.id+>ans)
{
ans=node[i].id-head.id+;
ansl=head.id;
ansr=node[i].id;
}
}
}
printf("%d %d\n",ansl,ansr);
return ;
}

CodeForces 616D Longest k-Good Segment的更多相关文章

  1. 【CodeForces 616D】Longest k-Good Segment

    题意 n个数里,找到最长的一个连续序列使里面最多k个不同的数. 分析 尺取法,每次R++,如果第R个数未出现过,那么不同的数+1,然后这个数的出现次数+1,如果不同的数大于k了,那就要去掉第L个数,直 ...

  2. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  3. codeforces gym 100971 K Palindromization 思路

    题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...

  4. Codeforces 632D Longest Subsequence 2016-09-28 21:29 37人阅读 评论(0) 收藏

    D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. codeforces 1269 E K Integers

    E. K Integers 题目连接:https://codeforces.com/contest/1269/problem/E 题意 给了一个排列p,你每次操作可以交换两个相邻的元素,现在问你最少操 ...

  6. Codeforces 700D - Huffman Coding on Segment(莫队+根分)

    Codeforces 题目传送门 & 洛谷题目传送门 好家伙,刚拿到此题时我连啥是 huffman 编码都不知道 一种对 \(k\) 个字符进行的 huffman 编码的方案可以看作一个由 \ ...

  7. Codeforces Gym 100523K K - Cross Spider 计算几何,判断是否n点共面

    K - Cross SpiderTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/v ...

  8. Codeforces 242E:XOR on Segment(位上的线段树)

    http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...

  9. 【codeforces 242E】XOR on Segment

    [原题题面]传送门 [题面翻译]传送门 [解题思路] 操作涉及到区间求和和区间异或,考虑到异或操作,我们对每个数二进制分解. 把每一位单独提出来做,异或要么取反要么变为不变,对于每一位建一颗线段树,那 ...

随机推荐

  1. php 控制循环

    控制结构语法 endif.endswitch.endwhile.endfor.endforeach

  2. rebar

    www.cnblogs.com/panfeng412/archive/2011/08/14/2137990.html

  3. opencv----彩色图像对比度增强

    图像对比度增强的方法可以分成两类:一类是直接对比度增强方法;另一类是间接对比度增强方法. 直方图拉伸和直方图均衡化是两种最常见的间接对比度增强方法. 直方图拉伸是通过对比度拉伸对直方图进行调整,从而“ ...

  4. boost库之graph入门

    #include <boost/graph/undirected_graph.hpp> #include <boost/graph/adjacency_list.hpp> us ...

  5. Learning Java IO indexes

    I/O Streams, it simplifies I/O operations, write a whole object out to stream & read back. File ...

  6. List、Set、Map集合存放null解析及HashMap、Hashtable异同点解析

    1.List.Set.Map集合存放null解析: @Test public void CollectionTest() { // 测试List List<Object> list = n ...

  7. 快学Scala-第三章 数组相关操作

    知识点: 1.定长数组 Array val nums = new Array[Int](10) //10个整数的数组,所有元素初始化为0 val a = new Array[String](10) / ...

  8. Hrbustoj 2266 Legendary Weights(辗转相除求最大公约数)

    题意:这个题目的意思是给出一些砝码,问我们能不能根据这些砝码称量出任意重量的物品,最大公约数并不难求,难的在于如何建立这个模型. 思路:根据数论的基础知识,两个数a,b的最大公约数是a*x + b*y ...

  9. cocos2d-x 那些常犯的错误

    Label::_label; if(_label==NULL){ //初始化_label的代码 } //指针默认值不等于NULL,需要赋初始值Label::_label=NULL; string st ...

  10. Ubuntu下载工具 uget+aria2

    一.安装. uget和aria2都可以在“软件中心”中安装,但是版本太老啦,无法发挥作用,所以最好还是在终端中添加ppa进行安装: 1.uget的安装:  sudo add-apt-repositor ...