Description

Luogu3069

USACO

Solution

由于两个点之间最多可以有\(k+1\)种牛,而牛的种数是单调的。所以可以用尺取法(区间伸缩法),每次右移右端点后,让左端点不断右移直到牛的种数不大于\(k+1\)就好了。

Code

#include <cstdio>
#include <algorithm> const int N = 100010; int n, k, a[N], b[N], c[N];
int col[N], tot, ans; int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
b[i] = a[i];
}
std::sort(b+1, b+1+n);
int nw = std::unique(b+1, b+1+n) - b;
for (int i = 1; i <= n; ++i) {
c[i] = std::lower_bound(b+1, b+1+nw, a[i]) - b;
}
int l = 1, r = 1;
while (r <= n) {
if (col[c[r++]]++ == 0) tot++;
while (tot > k+1) {
if (--col[c[l++]] == 0) tot--;
}
ans = std::max(ans, col[c[r-1]]);
}
printf("%d\n", ans);
return 0;
}

Note

当需要维护的性质满足区间单调的话,可以尝试尺取法。

[USACO13JAN]Cow Lineup的更多相关文章

  1. [USACO13JAN] Cow Lineup (单调队列,尺取法)

    题目链接 Solution 尺取法板子,算是复习一波. 题中说最多删除 \(k\) 种,那么其实就是找一个颜色种类最多为 \(k+1\) 的区间; 统计一下其中最多的颜色出现次数. 然后直接尺取法,然 ...

  2. H-The Cow Lineup(POJ 1989)

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

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

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

  4. bzoj 3048[Usaco2013 Jan]Cow Lineup 思想,乱搞 stl

    3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 168[Subm ...

  5. bzoj3048[Usaco2013 Jan]Cow Lineup 尺取法

    3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 225  Solved: 159[Subm ...

  6. BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针

    BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针 Description Farmer John's N cows (1 <= N <= 100,000) ...

  7. poj-1989 The Cow Lineup

    The Cow Lineup Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5587 Accepted: 3311 Descri ...

  8. [bzoj 3048] [Usaco2013 Jan]Cow Lineup

    [bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...

  9. [Luogu3069][USACO13JAN]牛的阵容Cow Lineup

    题目描述 Farmer John's N cows (1 <= N <= 100,000) are lined up in a row. Each cow is identified by ...

随机推荐

  1. RocketMQ的生产者和消费者

    生产者: /** * 生产者 */ public class Provider { public static void main(String[] args) throws MQClientExce ...

  2. C# NanUI WinFormium监听页面加载开始\结束

    个人博客 地址:https://www.wenhaofan.com/article/20190501213608 因为NanUI文档中仅介绍了Formium窗口的监听,但是没有WinFormium相关 ...

  3. 题解 AT4278 【[ABC115A] Christmas Eve Eve Eve】

    题目传送门. 分析 根据题目,我们可以发现要求如下: \(d\)的值 输出 \(d=25\) Christmas \(d=24\) Christmas Eve \(d=23\) Christmas E ...

  4. python数据分析学习(2)pandas二维工具DataFrame讲解

    目录 二:pandas数据结构介绍   下面继续讲解pandas的第二个工具DataFrame. 二:pandas数据结构介绍 2.DataFarme   DataFarme表示的是矩阵的数据表,包含 ...

  5. create_function()代码注入

    00x00create_function()函数的简介 适用范围:PHP 4> = 4.0.1,PHP 5,PHP 7 功能:根据传递的参数创建匿名函数,并为其返回唯一名称. 语法: creat ...

  6. Codeforce 567A - Lineland Mail

    All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its ...

  7. 题解【洛谷P2264】情书

    题面 看到每一单词在同一句话中出现多次感动值不叠加,一眼想到 \(\text{set}\). 首先将词汇列表中的单词存储起来,我用的是 \(\text{set}\). 对于每一个句子的单词,我们可以先 ...

  8. 多项式乘法逆元 - NTT

    递归求解即可 #include <bits/stdc++.h> using namespace std; #define int long long namespace NTT { #de ...

  9. Activiti工作流学习之SpringBoot整合Activiti5.22.0实现在线设计器(二)

    一.概述 网上有很多关于Eclipse.IDEA等IDE插件通过拖拽的方式来画工作流程图,个人觉得还是不够好,所以花点时间研究了一下Activiti在线设计器,并与SpringBoot整合. 二.实现 ...

  10. Centos 修改yum源为aliyun

    修改服务器源,避免长途跋涉到国外: 位置: vim  /etc/yum.repos.d/CentOS-Base.repo aliyun地址: 设置aliyun的yum源 wget -O /etc/yu ...