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;
}

  

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

  1. Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

    4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: ...

  2. BZOJ 4582 [Usaco2016 Open]Diamond Collector:贪心【相差不超过k】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4582 题意: 给你n个数. 让你将其中的一些数放入两个不同的集合中,并保证同一集合内两两元 ...

  3. bzoj4582[Usaco2016 Open]Diamond Collector

    bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装 ...

  4. [Usaco2016 Open]Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...

  5. BZOJ 4576: [Usaco2016 Open]262144

    Description 一个序列,每次可以将两个相同的数合成一个数,价值+1,求最后最大价值 \(n \leqslant 262144\) Sol DP. 这道题是 BZOJ 4580: [Usaco ...

  6. Diamond Collector

    Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...

  7. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

  8. Diamond Collector (动态规划)

    问题 I: Diamond Collector 时间限制: 1 Sec  内存限制: 64 MB提交: 22  解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, alwa ...

  9. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...

随机推荐

  1. BZOJ1856[SCOI2010]字符串

    Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgw ...

  2. canvas事件处理机制

    可以查看demo:http://sandbox.runjs.cn/show/hjb2hzzx(建议查看console查看点击后的改变) 具体原理是每次点击的时候去判断当前的鼠标坐标是属于哪一个路径下的 ...

  3. BZOJ 3505 【Cqoi2014】 数三角形

    Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个. 下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正整数m ...

  4. 使用axis2 soapmonitor监控soap数据

    Using the SOAP Monitor SOAPMonitor使用 使用SOAPMonitor axis2开发笔记-消息监控 WebService大讲堂之Axis2(10):使用soapmoni ...

  5. ubuntu12.04 安装CS:APP Y86模拟器

    下的第一UBUNTU12.04下Y86模拟器的安装:(參考http://archive.cnblogs.com/a/1865627/ 作适当改动) 1.安装bison和flex词法分析工具 sudo ...

  6. C# 如何生成一个时间戳

    在程序中,常常会用到时间戳,如何生成一个时间戳呢? /// <summary> /// 获取时间戳 /// </summary> /// <returns>< ...

  7. jQuery1.9之后使用on()绑定 动态生成元素的 事件无效

    来自互联网: 需要绑定a的父级元素(此元素必须为静态元素,不是后来动态生成的),然后设定on()方法的selector参数才行: $('p').on('mouseenter', 'a', functi ...

  8. 点击某个按钮在tableView某个位置动态插入一行cell

    实现步骤: 1.修改数据模型数组 给模型数组的某个位置增加一个模型 2.执行以下代码 NSIndexPath *indexPath = [NSIndexPath indexPathForRow: in ...

  9. Windows命令点滴

    1.删除windows服务项目: C:\服务器软件\Redis>sc delete Redis6379 [SC] DeleteService 成功 2.用于私网的IP地址段: 10.0.0.0/ ...

  10. New Features In SNMPv3 - SNMP Tutorial

    30.12 New Features In SNMPv3 We said that version 3 of SNMP represents an evolution that follows and ...