[Usaco2016 Open]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 th
em in a pair of display cases in the barn.Since Bessie wants the diamonds in each of the two cases t
o be relatively similar in size, she decides that she will not include two diamonds in the same case
if their sizes differ by more than K (two diamonds can be displayed together in the same case if th
eir sizes differ by exactly K). Given K, please help Bessie determine the maximum number of diamonds
she can display in both cases together.
给定长度为N的数列a,要求选出两个互不相交的子序列(可以不连续),满足同一个子序列中任意两个元素差的绝
对值不超过K。最大化两个子序列长度的和并输出这个值。1 ≤ N ≤ 50000, 1 ≤ a_i ≤ 10 ^ 9, 0 ≤ K ≤ 10^ 9
输入格式
The first line of the input file contains N and K (0≤K≤1,000,000,000). The next NN lines each cont
ain an integer giving the size of one of the diamonds. All sizes will be positive and will not excee
d 1,000,000,000
输出格式
Output a single positive integer, telling the maximum number of diamonds that Bessie can showcase in
total in both the cases.
样例输入
7 3
10
5
1
12
9
5
14
样例输出
5
提示
Silver鸣谢frank_c1提供翻译
思路
我或许被洛谷骗了,这不是splay的题。。。而是某种神奇的贪心。
代码实现
#include<cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s[],n,k,ans=;
int l[],r[];
int main(){
scanf("%d%d",&n,&k);
for (int i=;i<=n;++i) scanf("%d",&s[i]);
sort(s+,s+n+);
int h=; l[]=;
for (int i=;i<=n;++i){
while (s[i]-s[h]>k) h++;
l[i]=max(l[i-],i-h+);
}
r[n]=,h=n;
for (int i=n-;i>=;--i){
while(s[h]-s[i]>k) h--;
r[i]=max(r[i+],h-i+);
}
for (int i=;i<n;++i) ans=max(ans,l[i]+r[i+]);
printf("%d\n",ans);
}
[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: ...
- bzoj4582[Usaco2016 Open]Diamond Collector
bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装 ...
- BZOJ 4582: [Usaco2016 Open]Diamond Collector
Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这 ...
- BZOJ 4582 [Usaco2016 Open]Diamond Collector:贪心【相差不超过k】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4582 题意: 给你n个数. 让你将其中的一些数放入两个不同的集合中,并保证同一集合内两两元 ...
- Diamond Collector
Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- Diamond Collector (动态规划)
问题 I: Diamond Collector 时间限制: 1 Sec 内存限制: 64 MB提交: 22 解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, alwa ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 【BZOJ 4582】【Usaco2016 Open】Diamond Collector
http://www.lydsy.com/JudgeOnline/problem.php?id=4582 排好序后用两个指针直接\(O(n)\)扫,貌似这个东西学名"two pointers ...
随机推荐
- ionic之AngularJS扩展动态组件
目录: 1. 模态对话框 : $ionicModal 2. 上拉菜单 : $ionicActionSheet 3. 弹出框 : $ionicPopup 4. 浮动框 : $ionicPopover 5 ...
- BEM --Yandex的CSS 命名方法论
人们问我最多的问题之一是在CSS类名中--和__是什么意思?它们的出现是源于BEM和Nicolas Gallagher... BEM的意思就是块(block).元素(element).修饰符(modi ...
- EntityFramework:An error occurred while executing the command definition. See the inner exception for details.
错误描述: 调用EF中的FirstOrDefault()时,报错误:An error occurred while executing the command definition. See the ...
- 题解报告:hdu 1015 Safecracker
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kl ...
- Ajax动态加载数据
前言: 1.这个随笔实现了一个Ajax动态加载的例子. 2.使用.net 的MVC框架实现. 3.这个例子重点在前后台交互,其它略写. 开始: 1.控制器ActionResult代码(用于显示页面) ...
- 关于 VS 调用存储过程加载很慢和SQL 执行很快的那些事
执行同样的存储过程,调用同样的参数 在VS 中调用存储过程和传参后,到数据加载需要20秒或更多, 在SQL直接调用则不到一秒,同一个存储过程为什么有这么大的区别呢? 原因:存储过程计划失效的原因 产生 ...
- dede手机端首页点击文章内容、列表,却跳到pc端
手机访问到手机端首页,点击列表.内容.图片等都跳到pc端,是什么原因? 查看m模板里面的index.html文件生成的代码是绝对路径(数字随机)13.html 而不是view.php?aid=13 解 ...
- jQuery之基本选择器Practice
一.在输入框中输入数字,点击按钮,实现对应事件的功能. html代码: <input id="txt1" type="text" value=" ...
- linux环境下为php7装phpredis扩展
phpredis在php7.php5下都有不同的版本,装岔了可能会编译报错,所以在安装之前请先看下自己的php是啥版本. 我的redis装的是redis3.2.3版本. 用phpinfo()查看安装的 ...
- java虚拟机(二)--类加载机制和双亲委派模型
一.类的生命周期 加载(Loading).验证(Verification).准备(Preparation).解析(Resolution).初始化(Initialization).使用(Using).卸 ...