bzoj3048[Usaco2013 Jan]Cow Lineup 尺取法
3048: [Usaco2013 Jan]Cow Lineup
Time Limit: 2 Sec Memory Limit: 128 MB
Submit: 225 Solved: 159
[Submit][Status][Discuss]
Description
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.
Input
* Line 1: Two space-separated integers: N and K.
* Lines 2..1+N: Line i+1 contains the breed ID B(i).
Output
* Line 1: The largest size of a contiguous block of cows with identical breed IDs that FJ can create.
Sample Input
2
7
3
7
7
3
7
5
7
INPUT DETAILS: 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.
Sample Output
OUTPUT DETAILS: 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).
HINT
Source
尺取法
记录区间内每个元素出现次数和颜色种数
当种数<=k+1时r++,当种数>k+1时l++
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 100005
using namespace std;
int n,m,ans,num,a[N],b[N],cnt[N];
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n);
int len=unique(b+,b++n)-b-;
for(int i=;i<=n;i++)
a[i]=lower_bound(b+,b++len,a[i])-b;
int l=,r=;cnt[a[]]++;num++;
while(r<n){
while(r<n&&num<=m+){
if(!cnt[a[++r]])num++;
cnt[a[r]]++;
ans=max(ans,cnt[a[r]]);
}
while(num>m+&&l<=r)
if(!(--cnt[a[l++]]))num--;
}
printf("%d\n",ans);
return ;
}
bzoj3048[Usaco2013 Jan]Cow Lineup 尺取法的更多相关文章
- [bzoj3048] [Usaco2013 Jan]Cow Lineup
一开始一脸懵逼.. 后来才想到维护一左一右俩指针l和r..表示[l,r]这段内不同种类的数字<=k+1种. 显然最左的.合法的l随着r的增加而不减. 顺便离散化,记一下各个种类数字出现的次数就可 ...
- bzoj 3048[Usaco2013 Jan]Cow Lineup 思想,乱搞 stl
3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 237 Solved: 168[Subm ...
- BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针
BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针 Description Farmer John's N cows (1 <= N <= 100,000) ...
- [bzoj 3048] [Usaco2013 Jan]Cow Lineup
[bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...
- 【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希
题目描述 Farmer John has hired a professional photographer to take a picture of some of his cows. Since ...
- BZOJ 3048: [Usaco2013 Jan]Cow Lineup 双指针
看到这道题的第一个想法是二分+主席树(好暴力啊) 实际上不用这么麻烦,用一个双指针+桶扫一遍就行了 ~ code: #include <bits/stdc++.h> #define N 1 ...
- USACO 2011 November Cow Lineup /// map set 尺取法 oj25279
题目大意: 输入n 接下来n行描述n头牛的编号num和品种id 得到包含所有id的最短段 输出最短段的编号差 Sample Input 625 726 115 122 320 130 1 Sample ...
- [USACO13JAN] Cow Lineup (单调队列,尺取法)
题目链接 Solution 尺取法板子,算是复习一波. 题中说最多删除 \(k\) 种,那么其实就是找一个颜色种类最多为 \(k+1\) 的区间; 统计一下其中最多的颜色出现次数. 然后直接尺取法,然 ...
- BZOJ1636: [Usaco2007 Jan]Balanced Lineup
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 476 Solved: 345[ ...
随机推荐
- 利用python处理文档中各字段出现的次数并排序
import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...
- 利用python实现简单登陆注册系统
#!/usr/bin/env python # -*- coding:utf-8 -*- def login(username,password): ''' :param username:用户名 : ...
- Android属性动画 nineoldandroids
各种资源链接 nineoldandroids 任玉刚的五个图片滑动,点击menu http://blog.csdn.net/singwhatiwanna/article/details/1763998 ...
- 07-TypeScript的For循环
在传统的JavaScript中,关于循环,可以有两种方式,一种是forEach,一种是for. forEach的用法如下: var sarr=[1,2,3,4]; sarr.desc="he ...
- ( 转 ) CORS 有一次 OPTIONS 请求的原理
刚接触前端的时候,以为HTTP的Request Method只有GET与POST两种,后来才了解到,原来还有HEAD.PUT.DELETE.OPTIONS-- 目前的工作中,HEAD.PUT.DELE ...
- build.gradle & gradle.properties
一.build.gradle buildscript { ext { springBootVersion = '1.5.9.RELEASE' } repositories { maven { cred ...
- OAuth2.0学习(1-3)OAuth2.0的参与者和流程
OAuth(开放授权)是一个开放标准.允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息.而这种授权无需将用户提供用户名和密码提供给该第三方网站. OAuth允许用户提供一个令牌给第 ...
- python虚拟环境--virtualenv
virtualenv 是一个创建隔绝的Python环境的工具.virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包. 安装 pip install virtu ...
- 项目版本与分支管理之阿里AoneFlow模式分析
前言 在我前期的项目管理的经验中,一个项目需要维护多个产品及多个版本,这给版本与分支的管理增加了难度.前期没有重视,使得分支太多太乱,版本也没记录好,引发了很多的问题.在多种分支与版本的管理模式下,最 ...
- myeclipse自动添加注释
开发需要,新建类的时候,需要加自己的名字,每次都要自己写,嫌麻烦,修改一下myeclipse配置文件即可 打开window---preferences 选中 new Java files 点击edit ...