题目描述

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

最新文章

  1. 【Python②】python之首秀
  2. 第七篇——Mobile Apps,软件的曙光。
  3. 慕课网-Java入门第一季-7-2 Java 中无参无返回值方法的使用
  4. House Robber III leetcode 动态规划
  5. Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
  6. AngularJs ngIf、ngSwitch、ngHide/ngShow
  7. 无忧之道:Docker中容器的备份、恢复和迁移
  8. 滤镜与CSS3效果
  9. COM技术の接口
  10. c# 串行【序列化】和解串【反序列化】
  11. jquery冲突
  12. Mvc中把list从View传入Controller
  13. 直接拿来用 九个超实用的PHP代码片段(二)
  14. sizeof(int *) 和 sizeof(int)型的大小问题
  15. 【转】Python中执行cmd的三种方式
  16. SQL学习之空值(Null)检索
  17. Datatables快速入门开发--一款好用的JQuery表格插件
  18. .net开源工作流ccflow从表数据数据源导入设置
  19. vue安装过后遇到的坑
  20. redis 运维手册

热门文章

  1. 3-5 编程练习:jQuery实现简单的图片对应展示效果
  2. WebSphere Application Server切换JAVA SDK版本
  3. DataGridView 动态绑定 CheckBox
  4. OpenCV2.4.9 + VS2012 + win10 配置
  5. 四种IO模型
  6. EasyUI系列学习(十一)-Accordion(分类)
  7. log4j2异步日志解读(二)AsyncLogger
  8. 2017-12-04HTML table布局
  9. Android 新闻app的顶部导航栏,怎么实现动态加载?
  10. UUID 生成32位随机串