题目描述

Farmer John's N cows (1 <= N <= 100,000) are lined up in a row. Each cow is identified by an integer "breed ID" in the range 0...1,000,000,000; the breed ID of the ith cow in the lineup is B(i). Multiple cows can share the same breed ID.

FJ thinks that his line of cows will look much more impressive if there is a large contiguous block of cows that all have the same breed ID. In order to create such a block, FJ chooses up to K breed IDs and removes from his lineup all the cows having those IDs. Please help FJ figure out the length of the largest consecutive block of cows with the same breed ID that he can create by doing this.

农夫约翰的N(1 <= N <= 100,000)只奶牛排成了一队,每只牛都用编上了一个“血统编号”,该编号为范围0...1,000,000,000的整数。血统相同的奶牛有相同的编号,也就是可能有多头奶牛是相同的"血统编号"。

约翰觉得如果连续排列的一段奶牛有相同的血统编号的话,奶牛们看起来会更具有威猛。为了创造这样的连续段,约翰最多能选出k种血统的奶牛,并把他们全部从队列中赶走。

请帮助约翰计算这样做能得到的由相同血统编号的牛构成的连续段的长度最大是多少?

输入输出格式

输入格式:

* Line 1: Two space-separated integers: N and K.

* Lines 2..1+N: Line i+1 contains the breed ID B(i).

输出格式:

* Line 1: The largest size of a contiguous block of cows with

identical breed IDs that FJ can create.

输入输出样例

输入样例#1:

9 1
2
7
3
7
7
3
7
5
7
输出样例#1:

4

说明

There are 9 cows in the lineup, with breed IDs 2, 7, 3, 7, 7, 3, 7, 5, 7. FJ would like to remove up to 1 breed ID from this lineup.

By removing all cows with breed ID 3, the lineup reduces to 2, 7, 7, 7, 7, 5, 7. In this new lineup, there is a contiguous block of 4 cows with the same breed ID (7).


我们发现,如果一个区间的颜色数量小于等于$K + 1$的话,那么这一段区间的最大答案就是出现次数最多的数。

显然最左的、合法的l随着r的增加而不减。

所以直接区间扫过去就行了。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
#define reg register
#define gc getchar
inline int read() {
int res=;char ch=gc();bool fu=;
while(!isdigit(ch)){if(ch=='-')fu=;ch=gc();}
while(isdigit(ch))res=(res<<)+(res<<)+(ch^), ch=gc();
return fu?-res:res;
}
map <int, int> mp;
int tot;
int n, k;
int a[];
int ans;
int cnt[]; int main()
{
n = read(), k = read();
for (reg int i = ; i <= n ; i ++)
{
a[i] = read();
if (!mp[a[i]]) mp[a[i]] = ++tot;
a[i] = mp[a[i]];
}
int l = , r = ;
int num = ;
while(r <= n)
{
r ++;
if (!cnt[a[r]]) num++;
cnt[a[r]] ++;
if (num >= k + )
{
while(l <= r and num >= k + ) {
if (cnt[a[l]] == ) num--;
cnt[a[l]]--;
l++;
}
}
ans = max(ans, cnt[a[r]]);
}
cout << ans << endl;
return ;
}

[Luogu3069][USACO13JAN]牛的阵容Cow Lineup的更多相关文章

  1. LuoguP3069 【[USACO13JAN]牛的阵容Cow Lineup

    题目链接 看了看其他大佬的文章,为什么要控制右端呢 其实就是一个很简单的模拟队列趴... 难点就在于根据题意我们可以分析得一段合法区间内,不同种类个数不能超过k+2 哦当然,由于种类数范围过大,要对种 ...

  2. 洛谷P3069 [USACO13JAN]牛的阵容Cow Lineup(尺取法)

    思路 考虑比较朴素的解法,枚举每个长度为\(k+1\)的区间,然后统计区间中出现次数最多的颜色.这样的话复杂度为\(O(n*k)\)的,显然不行. 观察到统计每个区间中出现次数最多的颜色中,可以只用看 ...

  3. 【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希

    题目描述 Farmer John has hired a professional photographer to take a picture of some of his cows. Since ...

  4. 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup

    https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...

  5. 【题解】P3069 [USACO13JAN]牛的阵容Cow Lineup-C++

    题目传送门 思路这道题目可以通过尺取法来完成 (我才不管什么必须用队列)什么是尺取法呢?顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后 ...

  6. 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)

    题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...

  7. [USACO07JAN]平衡的阵容Balanced Lineup

    [USACO07JAN]平衡的阵容Balanced Lineup 题目描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) a ...

  8. H-The Cow Lineup(POJ 1989)

    The Cow Lineup Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5367   Accepted: 3196 De ...

  9. 3377: [Usaco2004 Open]The Cow Lineup 奶牛序列

    3377: [Usaco2004 Open]The Cow Lineup 奶牛序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 16  Solved ...

随机推荐

  1. http响应

    1.http响应 2.响应行常见状态码 200 :请求成功. 302 :请求重定向. 当访问网址A时,由于网址A服务器端的拦截器或者其他后端代码处理的原因,会被重定向到网址B. 304 :请求资源没有 ...

  2. Spring Boot 入门之单元测试篇(五)

    博客地址:http://www.moonxy.com 一.前言 JUnit 是一个由 Java 语言编写的开源的回归测试(回归测试是指重复以前全部或部分的相同测试)框架,由Erich Gamma 和 ...

  3. CabloyJS带你轻松走进NodeJS全栈开发-免费课程 作者亲授

    课程说明 B站直播 为回馈新老同学对开源框架CabloyJS的支持与厚爱,快速而轻松的开启NodeJS全栈开发之旅.2019年9月5日至9月11日在B站开启了一波免费直播培训课程 课程信息,请点击链接 ...

  4. SpringBoot——Web开发(静态资源映射)

    静态资源映射 SpringBoot对于SpringMVC的自动化配置都在WebMVCAutoConfiguration类中. 其中一个静态内部类WebMvcAutoConfigurationAdapt ...

  5. prometheus-operator告警模块alertmanager注意事项(QQ邮箱发送告警)--大大坑

    在/stable/prometheus-operator/values.yaml配置告警邮件 config: global: resolve_timeout: 5m smtp_smarthost: ' ...

  6. Linux之文件与目录管理

    加油!

  7. Sping学习笔记(一)----Spring源码阅读环境的搭建

    idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...

  8. Spring Boot + WebSocket 学习笔记

    首先需要了解一下背景,什么是WebSocket以及为什么要用WebSocket. 在常见的Web应用中,客户端与服务器通信,都是通过HTTP协议进行通信,客户端一次请求,服务端一次响应.而WebSoc ...

  9. 搭建Android+QT+OpenCV环境,实现“单色图片着色”效果

               OpenCV是我们大家非常熟悉的图像处理开源类库:在其新版本将原本在Contrib分库中的DNN模块融合到了主库中,并且更新了相应文档.这样我们就能够非常方便地利用OpenCV实 ...

  10. 微信小程序中的tabBar设置

    我们先来看一份图,这个设置在官方文档中已经写得很清楚了,我只是做一个总结 注:我写注释是为了方便说明,在小程序中的json文件中是不能用注释的 这个tabBar属于全局属性,因此就在全局配置文件app ...