题目描述

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 发布网站中遇到的几点问题

    1.windows 身份验证设置 打开IIS==>=>找到网站==> 身份验证==>打开功能==>启用windows身份验证 网站设置: 博客参考: http://blo ...

  2. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles

    There are nn rectangles on the plane. The problem is to find the area of the union of these rectangl ...

  3. Spring.Net学习笔记(2)-依赖注入

    一.开发环境 操作系统:Win10 编译器:VS2013 framework版本:.net 4.5 Spring版本:1.3.1 二.涉及程序集 Spring.Core.dll Common.Logg ...

  4. 6.12---前提两个对象的成员必须一致,才能将有数据的对象将数据传给反射获取的对象conver(有数据对象,目标对象)

    //// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)/ ...

  5. Python_购物车问题

    import os goods = [    {"name": "电脑", "price": 1999},    {"name&q ...

  6. 关于微信小程序:scroll-view,backgroundTextStyle

    踩过的坑mark下 1.滚动列表最好不要用scroll-view组件,这个组件有不少问题,比如触顶操作触发了,会连续好几次执行触发函数,得用一个开关变量和定时器配合来控制,一般情况下view组件就够用 ...

  7. Appium基于python unittest自动化测试并生成html测试报告

    本文基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告 代码示例: #利用unittest并生成测试报告 class Appium_test(uni ...

  8. Java图片上查找图片算法

    之前用按键精灵写过一些游戏辅助,里面有个函数叫FindPic,就是在屏幕范围查找给定的一张图片,返回查找到的坐标位置. 现在,Java来实现这个函数类似的功能. 算法描述: 屏幕截图,得到图A,(查找 ...

  9. 业余开发Android App的架构演变

    闲暇之余,开发了一款休闲类app,虽然用户量不多,但确实花了不少心血在这上面.然而,开发出来的结果,与之前想好的架构,还是有不少区别. 下面,记录下这款app架构的演变: 最初,只想写个app,能与机 ...

  10. [oracle]表空间情况查看、占用、扩容、使用情况、空间维护等操作

    --查询表空间使用情况SELECT Upper(F.TABLESPACE_NAME)         "表空间名",       D.TOT_GROOTTE_MB          ...