[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 ...
随机推荐
- redis 缓存应用
第1章 部署与安装 wget http://download.redis.io/releases/redis-3.2.10.tar.gz tar xf redis-3.2.10.tar.gz cd r ...
- ASP.NET SQL 总结(2)
Sql常见面试题(总结) 1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81 张三 数学 ...
- 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers
题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...
- Android 性能优化(22)*性能工具之「Hierarchy Viewer」 Hierarchy Viewer Walkthrough
Hierarchy Viewer Walkthrough 1.In this document Prerequisites Setting the ANDROID_HVPROTO variable W ...
- Android 性能优化(11)网络优化( 7)Optimizing for Doze and App Standby
Optimizing for Doze and App Standby In this document Understanding Doze Doze restrictions Adapting y ...
- Win7上安装Oracle数据库
由于ORACLE并没有FOR WIN7的版本,必须下载for vista_w2k8这个版本,将oralce 10G的安装镜像解压到硬盘,然后修改安装目录下的rehost.xml和oraparam.in ...
- 聊聊MyBatis缓存机制
https://tech.meituan.com/mybatis_cache.html 前言 MyBatis是常见的Java数据库访问层框架.在日常工作中,开发人员多数情况下是使用MyBatis的默认 ...
- 简单3步,你即可以用上myFocus
Step 1. 在html的标签内引入相关文件 <script type="text/javascript" src="js/myfocus-2.0.0.min.j ...
- js 获取json数组里面数组的长度
作为一个前端页面开发者第一次处理json数据,遇到了‘js 获取json数组里面数组的长度’?竟然不知道 json没有.length属性(真是要嘲讽下自己),少壮不努力老大徒伤悲啊!以前都是去寻求男朋 ...
- XAMPP--Apache服务无法启动问题定位及处理
一.问题简述: XAMPP 在使用一段时间后,Apache服务无法启动. 二.详细描述: 安装Xampp服务器套件之后,部署使用正常.一段时间未使用,再次打开时,Apache服务无法启动.错误提示如下 ...