[Luogu3069][USACO13JAN]牛的阵容Cow Lineup
题目描述
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.
输入输出样例
- 9 1
- 2
- 7
- 3
- 7
- 7
- 3
- 7
- 5
- 7
- 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的更多相关文章
- LuoguP3069 【[USACO13JAN]牛的阵容Cow Lineup
题目链接 看了看其他大佬的文章,为什么要控制右端呢 其实就是一个很简单的模拟队列趴... 难点就在于根据题意我们可以分析得一段合法区间内,不同种类个数不能超过k+2 哦当然,由于种类数范围过大,要对种 ...
- 洛谷P3069 [USACO13JAN]牛的阵容Cow Lineup(尺取法)
思路 考虑比较朴素的解法,枚举每个长度为\(k+1\)的区间,然后统计区间中出现次数最多的颜色.这样的话复杂度为\(O(n*k)\)的,显然不行. 观察到统计每个区间中出现次数最多的颜色中,可以只用看 ...
- 【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希
题目描述 Farmer John has hired a professional photographer to take a picture of some of his cows. Since ...
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
- 【题解】P3069 [USACO13JAN]牛的阵容Cow Lineup-C++
题目传送门 思路这道题目可以通过尺取法来完成 (我才不管什么必须用队列)什么是尺取法呢?顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后 ...
- 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)
题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...
- [USACO07JAN]平衡的阵容Balanced Lineup
[USACO07JAN]平衡的阵容Balanced Lineup 题目描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) a ...
- H-The Cow Lineup(POJ 1989)
The Cow Lineup Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5367 Accepted: 3196 De ...
- 3377: [Usaco2004 Open]The Cow Lineup 奶牛序列
3377: [Usaco2004 Open]The Cow Lineup 奶牛序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 16 Solved ...
随机推荐
- 在64位Linux上安装32位gmp大数库
前期准备: 如果没有安装32位gcc和g++环境的话,可能会导致安装失败,此时请参考上一篇博文 http://www.cnblogs.com/weir007/p/5977759.html,根据系统版本 ...
- unittest核心要素
1 TestCase 一个TestCase的实例就是一个测试用例.什么是测试用例呢?就是一个完整的测试流程, 包括测试环境的准备(setUp),执行测试代码(run),以及测试后环境的还原(tearD ...
- 干货| 外卖点餐系统(App及后台)
简单总结一下,这个系统是一个外卖点餐系统,也就是仿美团饿了么,也是当时我的毕业设计,花费了我很多精力,主要包括了移动端App.服务端(中台)和商家的后台管理.答辩完后我就将移动端源码放到了GitHub ...
- python pandas进行条件筛选时出现ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().”
在使用pandas进行条件筛选时,使用了如下的代码: fzd_index=data[(data['实际辐照度']<mi)or(data['实际辐照度']>ma)].index 原本以为,并 ...
- leetcode - 最小移动次数使数组元素相等
思路 (假设数组每次都已被排好序) 每次使得小于等于最大值的n-1的数字加1,直接暴力求解会超时 改进一: 为了让最小元素等于最大元素,至少需要数组中最大值-最小值次, 所以以此为基础再次暴力求解(参 ...
- windows下安装vue教程
前言:前段时间学习了下vue,也算是能简单开发了,今天就记录下怎么通过vue-cli来安装vue. 因vue是基于node环境的,如果你还不会安装的话,可以看下我的这个教程:安装node.js和npm ...
- Java中对象创建时的内存分配
一.前言知识铺垫 1.逃逸对象:在一个方法内创建的对象没有被外界引用则称该对象为未逃逸的对象. 2.JDK1.6以后的HotSpot虚拟机支持运行时的对象逃逸分析. 3.JVM中的参数配置: 1) ...
- DOM之事件(二)
今天详细讲解JavaScript中的常用事件类型和功能. 一 鼠标事件 1, click:点击事件 等同于mousedown+mouseup,不管这两个事件间隔多久,都会触发一次click事件. 2 ...
- Redis 5.0 redis-cli --cluster help说明
背景: Redis Cluster 在5.0之后取消了ruby脚本 redis-trib.rb的支持(手动命令行添加集群的方式不变),集合到redis-cli里,避免了再安装ruby的相关环境.直接使 ...
- Redis连接池-Java代码
1.JedisUtil类 2.测试类 3.测试日志(模拟出现竞争情况) import org.apache.log4j.Logger; import redis.clients.jedis.Jedis ...