[bzoj 3048] [Usaco2013 Jan]Cow Lineup

Description

给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿,现在给你一个k,表示你最多可以删去k类数。数列中相同的数字被称为一类数。设该数列中满足所有的数字相等的连续子序列被叫做完美序列,你的任务就是通过删数使得该数列中的最长完美序列尽量长。

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.

由于每个数都有10亿那么大,所以我们需要先离散化.之后我们维护一个队列.(好吧如果它也可以叫做单调队列)我们需要维护这个队列中不同数的个数<=k. 整个数列中的答案就可能是这个队列中某类数的出现次数.我们最多到k+1个数的时候就需要将队头像右移.答案其实等价于队列中有k+1类数,删除k类数留下来的同一类数的个数,那么显然队列中的每一类数的个数都有可能成为答案,所以我们在队列中就可以维护最大答案了.

贴上代码

#include <map>
#include <cstdio>
#include <algorithm>
using namespace std; static const int maxm=1e6+10; int A[maxm],Q[maxm],cnt[maxm];
int n,k,x,pos=1,head=1,tail=1,tot,kind,ans; map<int,int>M; int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&x);
if(!M[x])M[x]=++tot;
A[i]=M[x];
} while(pos<=n){
while(head<=tail&&kind<=k&&pos<=n){
if(!cnt[A[pos]])kind++;
cnt[A[pos]]++;
ans=max(ans,cnt[A[pos]]);
Q[tail++]=A[pos++];
}
while(head<=tail&&kind==k+1&&pos<=n){
if(!cnt[A[pos]])break;
cnt[A[pos]]++;
ans=max(ans,cnt[A[pos]]);
Q[tail++]=A[pos++];
}
if(!--cnt[Q[head++]])kind--;
} printf("%d\n",ans); return 0;
}

传送门(bzoj权限题,附上另一OJ)

P.S:由于我提交的OJ的数据似乎有些问题,所以代码只是与标程对拍了,如果有误,欢迎指正

[bzoj 3048] [Usaco2013 Jan]Cow Lineup的更多相关文章

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

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

  2. BZOJ 3048: [Usaco2013 Jan]Cow Lineup 双指针

    看到这道题的第一个想法是二分+主席树(好暴力啊) 实际上不用这么麻烦,用一个双指针+桶扫一遍就行了 ~ code: #include <bits/stdc++.h> #define N 1 ...

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

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

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

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

  5. [bzoj3048] [Usaco2013 Jan]Cow Lineup

    一开始一脸懵逼.. 后来才想到维护一左一右俩指针l和r..表示[l,r]这段内不同种类的数字<=k+1种. 显然最左的.合法的l随着r的增加而不减. 顺便离散化,记一下各个种类数字出现的次数就可 ...

  6. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  7. Bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 传递闭包,bitset

    1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 891  Solved: 590 ...

  8. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  9. BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛( floyd )

    对于第 i 头牛 , 假如排名比它高和低的数位 n - 1 , 那么他的 rank 便可以确定 . floyd -------------------------------------------- ...

随机推荐

  1. oracle 多行数据合并一行数据

    在工作中遇见的oracle知识,多行合并成一行,记录一下 1.取出需要的数据,代码: (SELECT to_char(m.f_meetdate, 'yyyy-MM-dd'), decode(nvl(m ...

  2. Drop it-freecodecamp算法题目

    Drop it 1.要求 丢弃数组(arr)的元素,从左边开始,直到回调函数return true就停止. 第二个参数,func,是一个函数.用来测试数组的第一个元素,如果返回fasle,就从数组中抛 ...

  3. k8s的service简述

    k8s向集群外部暴露端口的3种方式: 1.service->nodePort :仅暴露一个宿主机端口,用于集群外部访问,因为此操作被写入各个节点的iptables或ipvs规则当中,可以用任意一 ...

  4. 2019年Vue学习路线图

    https://juejin.im/entry/5c108864f265da61726555ed 官网: https://cn.vuejs.org/index.html js引入地址 https:// ...

  5. Head First Python (一)

    建立一个数组: cast = ["Cleese","Palin","Jones","Idle"] 列出数组有多少数据项: ...

  6. 洛谷P2389 电脑班的裁员(区间DP)

    题目背景 隔壁的新初一电脑班刚考过一场试,又到了BlingBling的裁员时间,老师把这项工作交给了ZZY来进行.而ZZY最近忙着刷题,就把这重要的任务交(tui)给了你. 题目描述 ZZY有独特的裁 ...

  7. 使用 Sconfig.cmd 配置服务器核心服务器

    使用 Sconfig.cmd 配置服务器核心服务器 适用对象:Windows Server 2012 R2, Windows Server 2012 在 Windows Server 2012 中,你 ...

  8. 3、CSS基础 part-1

    1.给body设置颜色 <html> <body text="red"> <p> hello world</p> <p> ...

  9. 35、键盘布局的tableLayout备份

    <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_conte ...

  10. Python-S9——Day110-Git继续

    1 当日内容概要 2 内容回顾 3 Git版本控制之多人协同开发 4 Git版本控制之fork 5 版本控制之其他 6 Redis之字典基本操作 7 Django中操作Redis 8 Django缓存 ...