[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.

输入输出样例

输入样例#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=0;char ch=gc();bool fu=0;
    while(!isdigit(ch)){if(ch==‘-‘)fu=1;ch=gc();}
    while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48), ch=gc();
    return fu?-res:res;
}
map <int, int> mp;
int tot;
int n, k;
int a[100005];
int ans;
int cnt[100005];

int main()
{
    n = read(), k = read();
    for (reg int i = 1 ; i <= n ; i ++)
    {
        a[i] = read();
        if (!mp[a[i]]) mp[a[i]] = ++tot;
        a[i] = mp[a[i]];
    }
    int l = 1, r = 0;
    int num = 0;
    while(r <= n)
    {
        r ++;
        if (!cnt[a[r]]) num++;
        cnt[a[r]] ++;
        if (num >= k + 2)
        {
            while(l <= r and num >= k + 2) {
                if (cnt[a[l]] == 1) num--;
                cnt[a[l]]--;
                l++;
            }
        }
        ans = max(ans, cnt[a[r]]);
    }
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/BriMon/p/9784418.html

时间: 2024-10-20 05:31:57

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

P2966 [USACO09DEC]牛收费路径Cow Toll Paths

P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The cows mo

bzoj3048[Usaco2013 Jan]Cow Lineup*

bzoj3048[Usaco2013 Jan]Cow Lineup 题意: 给你一个序列,你最多可以删去k类数(数列中相同的数字被称为一类数).求通过删数得到的该序列中的最长完美序列(满足所有的数字相等的连续子序列被叫做完美序列).序列大小≤100000 题解: 先离散化,然后维护一个单调队列,如果当前类数小于等于k+1就比较答案,否则左端点++. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorit

编程算法 - 最好牛线(Best Cow Line) 代码(C)

最好牛线(Best Cow Line) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定长度为N的字符串S, 要构造一个长度为N的字符串T. 反复进行如下任意操作. 从S的头部删除一个字符, 放入T的尾部; 从S的尾部删除一个字符, 放入T的尾部; 目标是要构造字典序尽可能小的字符串T. 使用贪心算法, 不断选取S首尾最小的字符, 放入T, 如果相等, 则再次向内查找, 找到内部最小的. 代码: /* * main.cpp * * Cr

洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic dfs 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,k,p[10000],can[10000]; 4 int w[1000+15][1000+15]; 5 bool vis[10000]; 6 7 void dfs(int pre) 8 { 9 for(int j=1;j<=n;j++) 10 { 11 if(w[pre][j]&&!

洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way path

P2880 [USACO07JAN]平衡的阵容Balanced Lineup(RMQ的倍增模板)

题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ问题:给定一个长度为N的区间,M个询问,每次询问Li到Ri这段区间元素的最大值/最小值. RMQ的高级写法一般有两种,即为线段树(并不很会╥﹏╥...)和ST表(一种利用dp求解区间最值的倍增算法) 定义:maxx[i][j]和minn[i][j]分别表示i到i+2^j-1这段区间的最大值和最小值. 预处理:maxx[i][0]=minn[i][0]=a[i].即i到i区间的最大值.最小值都是a[i]. 状

洛谷 P2909 [USACO08OPEN]牛的车Cow Cars

P2909 [USACO08OPEN]牛的车Cow Cars 题目描述 N (1 <= N <= 50,000) cows conveniently numbered 1..N are driving in separate cars along a highway in Cowtopia. Cow i can drive in any of M different high lanes (1 <= M <= N) and can travel at a maximum speed

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads co

P2880 [USACO07JAN]平衡的阵容Balanced Lineup

P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ RMQ模板题 静态求区间最大/最小值 (开了O2还能卡到rank9) #include<iostream> #include<cstdio> #include<cstring> #include<cctype> using namespace std; template <typename T> inline T min(T &a,T &b) {