#P3143. [USACO16OPEN] Diamond Collector S

    ID: 2187 Type: RemoteJudge 1000ms 128MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>贪心2016USACO枚举双指针,two-pointer

[USACO16OPEN] Diamond Collector S

题目描述

Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected NN diamonds (N50,000N \leq 50,000) of varying sizes, and she wants to arrange some of them in a pair of display cases in the barn.

Since Bessie wants the diamonds in each of the two cases to 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 KK (two diamonds can be displayed together in the same case if their sizes differ by exactly KK). Given KK, please help Bessie determine the maximum number of diamonds she can display in both cases together.

输入格式

The first line of the input file contains NN and KK (0K1,000,000,0000 \leq K \leq 1,000,000,000).

The next NN lines each contain an integer giving the size of one of the diamonds. All sizes will be positive and will not exceed 1,000,000,0001,000,000,000.

输出格式

Output a single positive integer, telling the maximum number of diamonds that Bessie can showcase in total in both the cases.

题目大意

题目描述

奶牛 Bessie 一直喜欢闪闪发光的物体,她最近在业余时间开始了一项爱好——挖掘钻石!她收集了 NN 颗大小各不相同的钻石(N50,000N \leq 50,000),并希望将它们中的一部分放在谷仓里的两个展示柜中展示。

由于 Bessie 希望每个展示柜中的钻石大小相对接近,她决定如果两颗钻石的大小相差超过 KK,就不能将它们放在同一个展示柜中(如果两颗钻石的大小相差恰好为 KK,则可以将它们一起展示在同一个展示柜中)。给定 KK,请帮助 Bessie 确定她可以在两个展示柜中一起展示的最大钻石数量。

输入格式

输入文件的第一行包含 NNKK0K1,000,000,0000 \leq K \leq 1,000,000,000)。

接下来的 NN 行每行包含一个整数,表示一颗钻石的大小。所有钻石的大小均为正数且不超过 1,000,000,0001,000,000,000

输出格式

输出一个正整数,表示 Bessie 可以在两个展示柜中一起展示的最大钻石数量。

7 3
10
5
1
12
9
5
14
5