题目描述

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的更多相关文章

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

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

  2. bzoj4582[Usaco2016 Open]Diamond Collector

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

  3. BZOJ 4582: [Usaco2016 Open]Diamond Collector

    Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这 ...

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

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

  5. Diamond Collector

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

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

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

  7. Diamond Collector (动态规划)

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

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

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

  9. 【BZOJ 4582】【Usaco2016 Open】Diamond Collector

    http://www.lydsy.com/JudgeOnline/problem.php?id=4582 排好序后用两个指针直接\(O(n)\)扫,貌似这个东西学名"two pointers ...

随机推荐

  1. .net引用System.Data.SQLite操作SQLite

    之所以要做这个笔记,是因为在.NET中使用System.Data.SQLite的时候,遇到了些问题,这些问题是相对于引用其他dll没有遇到过的,所以作个笔记,记录一下. 简单起见,首先建立一个控制台项 ...

  2. node-rsa加密,java解密调试

    用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具类加密解密正常.但是用node加密玩的java解密不了.原因:node默认的是 DEFAULT_ENCRYPTION_SCHEM ...

  3. SQL优化器简介

    文章导读: 什么是RBO? 什么是CBO? 我们在工作中经常会听到这样的声音:"SQL查询慢?你给数据库加个索引啊".虽然加索引并不一定能解决问题,但是这初步的体现了SQL优化的思 ...

  4. C#随机取部分数据

    1.使用Random伪随机生成器 但是这样会由于转换为数组类型导致性能下降,千万要避免这种用法. 2.使用Take返回重头开始指定数量的连续元素 每次进来这个方法的时候,都使用Guid进行一次排序,然 ...

  5. 全志tina v3.0系统编译时的时间错误的解决(全志SDK的维护BUG)

    全志tina v3.0系统编译时的时间错误的解决(全志SDK的维护BUG) 2018/6/13 15:52 版本:V1.0 开发板:SC3817R SDK:tina v3.0 1.01原始编译全志r1 ...

  6. linux使用crontab实现PHP执行计划定时任务

    linux使用crontab实现PHP执行计划定时任务 前几天写过一篇文章,利用单纯的php实现定时执行任务,但是效率不佳,对于linux来说用crontab实现更加合理 首先说说cron,它是一个l ...

  7. StyleAI厚积薄发: Android网络图片数据传输

    在StyleAI上厚积了这么长时间,憋了这么久,本来想憋个更大的,不过还是薄发一次的好. 三.直接使用别人的工程 文章:Android学习之客户端上传图片到服务器 下载地址:https://downl ...

  8. 【转】WITH AS 用法

    转载自:http://blog.csdn.net/shaochao14/article/details/6223052 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery ...

  9. OpenTSDB监控

    OpenTSDB监控

  10. jquery选择器,筛选器,属性,事件 基础

    左边栏实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...