You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume vj of barrel j be equal to the length of the minimal stave in it.
You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).
The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
4 2 1 2 2 1 2 3 2 2 3
7
2 1 0 10 10
20
1 2 1 5 2
2
3 2 1 1 2 3 4 5 6
0
题意:现在给你n*k个数,要你构造成n组,每组有k个数,将每组中的最小的数字相加,即为所求解,现在问你最大的解是多少,同时要保证每组最小值之差不超过l。
思路:贪心即可,我们必然会选取n个数,在保证这n个数任意之差不超过l的前提下,选出最大和。
1.首先排个序,那么我们所有可选数字的范围:[a[1],a[1]+l],很明显,我们要选取的n个数字,就在这个范围中,如果这个范围数字个数小于n,即不可能有答案。
2.假设当前可选的数字范围最右端的下标是pos,即可选的数字个数是pos,剩下的个数num=n*k-pos,这num个数是待匹配的,此时从pos开始,向左逐个选取,当做每组的最小值,每选一个,pos--,num-k一次,当不够减时,也就说明不能连续选取了,右边的已经不够匹配了。
3.此时开始每隔k个间隔选取一次,当从开始到现在选取够n-1个数字时,可以停下了,因为n*k个数中最小的一个a[1]必然要入选,在最后sum+=a[1]即可,当然如果选不够n-1个,直接判定不存在选取方案。
ps:有一个特判,当pos>=n-k+1时,此时是最简单,所有方案中的最优解,即:a[1],a[1+k],a[1+2*k]...,求和输出即可,想还是很好想,就是bug太多,代码实现有点麻烦。
#include
#include
#include
#include
#include
#include
#include
#include