BZOJ 4582: [Usaco2016 Open]Diamond Collector

Descrirption

给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\)

Sol

二分+DP.

排一下序,找出以这个点结尾和开始的位置.

这个玩意可以二分也可以用单调队列,随便搞啊...

然后统计答案就是枚举第二个序列的起点,然后往后扫的时候统计一下,第一个序列的最大长度就可以了.

Code

/**************************************************************
    Problem: 4582
    User: BeiYu
    Language: C++
    Result: Accepted
    Time:76 ms
    Memory:1876 kb
****************************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

const int N =50050;

int n,k,ans;
int a[N];
int f[N],g[N];

inline int in(int x=0){ scanf("%d",&x);return x; }
int uppp(int x){// >=
    int l=1,r=n,mid;
    for(;l<=r;){
        mid=(l+r)>>1;
        if(a[mid] < x) l=mid+1;
        else r=mid-1;
    }return l;
}
int lwww(int x){//<=
    int l=1,r=n,mid;
    for(;l<=r;){
        mid=(l+r)>>1;
        if(a[mid] <= x) l=mid+1;
        else r=mid-1;
    }return r;
}
int main(){
    n=in(),k=in();
    for(int i=1;i<=n;i++) a[i]=in();
    sort(a+1,a+n+1);

    for(int i=1;i<=n;i++){
        f[i]=i-uppp(a[i]-k)+1;
        g[i]=lwww(a[i]+k)-i+1;
    }

//  for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
//  for(int i=1;i<=n;i++) cout<<f[i]<<" ";cout<<endl;
//  for(int i=1;i<=n;i++) cout<<g[i]<<" ";cout<<endl;

    for(int i=1;i<=n;i++){
        ans=max(ans,f[i-1]+g[i]);
        f[i]=max(f[i],f[i-1]);
    }
    cout<<ans<<endl;
    return 0;
}

  

时间: 2024-10-07 05:02:02

BZOJ 4582: [Usaco2016 Open]Diamond Collector的相关文章

Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 136[Submit][Status][Discuss] Description Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! Sh

4582: [Usaco2016 Open]Diamond Collector

4582: [Usaco2016 Open]Diamond Collector Description Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected N diamonds (N≤50,000) of varying sizes, and she wants to arrange some of t

bzoj4582[Usaco2016 Open]Diamond Collector

bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装多少个.n最大50000. 题解: 我真傻,真的~首先对大小排序,然后找以i为左端点的可装区间,这个操作两个指针就可以搞,我却以为要二分查找.预处理完了,因为不交错的区间肯定比交错的区间优,所以从n到1递推一下从n到i最大的区间大小是多少,然后枚举每个区间,找到当前区间大小加上右端点+1到n中最大的

BZOJ 4576: [Usaco2016 Open]262144

Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco2016 Open]248 加强版. 做248的那个区间DP其实很多方案都是0,而且一个区间中只有一个合法的数字. 然后就是 一个区间合成一个数的方案的这个数字是固定的. \(f[i][j]\) 表示以 \(i\) 结尾是否能合成 \(j\),同时记录一下转移位置,每次向前找前一个指针就可以了. 复

Diamond Collector (动态规划)

问题 I: Diamond Collector 时间限制: 1 Sec  内存限制: 64 MB提交: 22  解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected N diamonds (N≤50,000) of varying sizes, and sh

Diamond Collector

Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected N diamonds (N≤50,000) of varying sizes, and she wants to arrange some of them in a pair of display case

P3143 [USACO16OPEN]钻石收藏家Diamond Collector[two-pointers]

P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题意要注意一点:有两个陈列架! 如果只有一个陈列架,是很容易的.two-pointers直接从左到右跑一下即可. 如果有两个陈列架,就需要进行答案合并了.做法是这样的: 设两个数组: \(pre\)数组,\(pre[i]\)表示以\([1,i]\)为右端点时区间长度的最大值. \(suf\)数组,\(suf[i]\)表示以\([i,n]\)为左端点时区间长度的最大值. 两个数组分别是前缀最大值和后缀最大值.同样利

BZOJ 4742: [Usaco2016 Dec]Team Building

4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Submit][Status][Discuss] Description Every year, Farmer John brings his NN cows to compete for "best in show" at the state fair. His arch -rival, F

bzoj 4412: [Usaco2016 Feb]Circular Barn

4412: [Usaco2016 Feb]Circular Barn Description 有一个N个点的环,相邻两个点距离是1.点顺时针标号为1..N.每一个点有ci头牛,保证∑ci=N.每头牛都可以顺时针走.设一头牛走了d个单位停下了,将耗费d^2的能量.请设计一种牛的走法,使得每一个点上都正好有一头牛,且最小化耗费的能量. Input 第一行一个数N.N <= 100000接下来N行,每行一个数ci. Output 输出一个数表示耗费能量的最小值 Sample Input 10 1 0