bzoj3048[Usaco2013 Jan]Cow Lineup*

bzoj3048[Usaco2013 Jan]Cow Lineup

题意:

给你一个序列,你最多可以删去k类数(数列中相同的数字被称为一类数)。求通过删数得到的该序列中的最长完美序列(满足所有的数字相等的连续子序列被叫做完美序列)。序列大小≤100000

题解:

先离散化,然后维护一个单调队列,如果当前类数小于等于k+1就比较答案,否则左端点++。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 100010
 6 using namespace std;
 7
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();}
11     while(ch>=‘0‘&&ch<=‘9‘)x=x*10+ch-‘0‘,ch=getchar();
12     return f*x;
13 }
14 struct ls{int id,v;}; ls lss[maxn]; int tot,v[maxn],n,k,l,r,cnt[maxn],mx,kinds;
15 bool cmp(ls a,ls b){return a.v<b.v;}
16 int main(){
17     n=read(); k=read(); inc(i,1,n)lss[i]=(ls){i,read()}; sort(lss+1,lss+n+1,cmp);
18     inc(i,1,n){if(i==1||lss[i].v!=lss[i-1].v)tot++; v[lss[i].id]=tot;}
19     l=1; r=1; kinds=0;
20     while(1){
21         while(r<=n&&kinds<=k+1){
22             cnt[v[r]]++; if(cnt[v[r]]==1)kinds++; mx=max(mx,cnt[v[r]]); r++;
23         }
24         if(r==n+1)break;
25         while(kinds>k+1){
26             cnt[v[l]]--; if(!cnt[v[l]])kinds--; l++;
27         }
28     }
29     printf("%d",mx); return 0;
30 }

20160814

时间: 2024-08-07 21:41:12

bzoj3048[Usaco2013 Jan]Cow Lineup*的相关文章

[BZOJ] 1636: [Usaco2007 Jan]Balanced Lineup

1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 909  Solved: 644[Submit][Status][Discuss] Description For the daily milking, Farmer John's N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer

BZOJ1636: [Usaco2007 Jan]Balanced Lineup

1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[Submit][Status] Description For the daily milking, Farmer John's N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer John deci

bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的

[bzoj 1699] [Usaco2007 Jan]Balanced Lineup排队

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1699 [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1443  Solved: 920[Submit][Status][Discuss] Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一

BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 568[Submit][Status] Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. Jo

[Usaco2007 Jan]Balanced Lineup排队

[Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q &l

【BZOJ 3048】【USACO2013 Jan】Cow Lineup 滑块思想

昨天下午想了好久没想出来,果然是很弱,思考能力低下. 用的类似单调队列的思想,维护一个长度为$k+1$的滑块,每次统计下$ans$就可以了 #include<cstdio> #include<algorithm> using namespace std; int n, k, H[100003], id[100003], a[100003], cnt = 0, c[100003], st, ans = 0; inline bool cmp(int X, int Y) { return

[bzoj1612][Usaco2008 Jan]Cow Contest奶牛的比赛_dfs

Cow Contest奶牛的比赛 bzoj-1612 Usaco-2008 Jan 题目大意:题目链接. 注释:略. 想法: 我们对于每个点dfs,看一下比这个点大的点加上比这个点小的点是否是n-1即可. 最后,附上丑陋的代码... ... #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N

[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