Liebig's Barrels

Problem

  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.

Input

  The first line contains three space-separated integers nk and l (1 ≤ n, k ≤ 1051 ≤ n·k ≤ 1050 ≤ l ≤ 109).

  The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.

Output

  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.

Examples
  input
    4 2 1
    2 2 1 2 3 2 2 3
  output
    7
  input
    2 1 0
    10 10
  output
    20
  input
    1 2 1
    5 2
  output
    2
  input
    3 2 1
    1 2 3 4 5 6
  output
    0

Note

  In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].

  In the second example you can form the following barrels: [10], [10].

  In the third example you can form the following barrels: [2, 5].

  In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.

 

 

 
题目大意
  给m = n × k块木板,每块木板长ai。要求组成n个桶,每个桶k块木板,每个桶容积数值与组成其所有木板中最短的那块木板长度相等,在任意两个桶容积差不超过l的前提下,问所有桶的容积和最大是多少(若要求不可能满足则输出0)。

 

算法

  将木板按长度从小到大排序得{ap[i]}。

  若不考虑l则答案即为sum{ap[i × k]}。

  注意,n个桶中容积最小的桶必包含长度最短的木板。因此,这n个桶的容积范围D == [ap[0], ap[0] + l]。当且仅当长度范围在D内的木板数量少于n个,要求不可能被满足。

  当要求可以满足时,对于从大到小的每个ap[i × k],若ap[i × k]不在D内则将其与长度在D内且尚未被交换过的最大木板交换位置。设该操作后的木板长度序列为{aq[i]}。

  答案:

    sum{aq[i × k]}

  时间复杂度:

    O(mlog(m))

  空间复杂度:

    O(m)

 
代码
 1 def doit(n, k, l, a):
 2     m = n * k
 3     a.sort()
 4     hen = 0
 5     tai = m
 6     while ((hen < m) and (a[hen] <= a[0] + l)):
 7         hen += 1
 8     if (hen < n):
 9         return 0
10     for i in range(n):
11         tai -= k
12         if (hen > tai):
13             break
14         hen -= 1
15         a[hen], a[tai] = a[tai], a[hen]
16     temp = 0
17     for i in range(n):
18         temp += a[i * k]
19     return temp
20     
21 n, k, l = input().split()
22 a = input().split()
23 for i in range(len(a)):
24     a[i] = int(a[i])
25 print(doit(int(n), int(k), int(l), a))

猜你喜欢

转载自www.cnblogs.com/Efve/p/9196921.html