CF1690E.Price Maximization

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A batch of nn goods (nn — an even number) is brought to the store, ii-th of which has weight aia_i. Before selling the goods, they must be packed into packages. After packing, the following will be done:

  • There will be n2\frac{n}{2} packages, each package contains exactly two goods;
  • The weight of the package that contains goods with indices ii and jj (1≤i,j≤n1 \le i, j \le n) is ai+aja_i + a_j.

With this, the cost of a package of weight xx is always ⌊xk⌋\left \lfloor\frac{x}{k}\right\rfloor burles (rounded down), where kk — a fixed and given value.

Pack the goods to the packages so that the revenue from their sale is maximized. In other words, make such n2\frac{n}{2} pairs of given goods that the sum of the values ⌊xik⌋\left \lfloor\frac{x_i}{k} \right \rfloor, where xix_i is the weight of the package number ii (1≤i≤n21 \le i \le \frac{n}{2}), is maximal.

For example, let n=6,k=3n = 6, k = 3, weights of goods a=[3,2,7,1,4,8]a = [3, 2, 7, 1, 4, 8]. Let's pack them into the following packages.

  • In the first package we will put the third and sixth goods. Its weight will be a3+a6=7+8=15a_3 + a_6 = 7 + 8 = 15. The cost of the package will be ⌊153⌋=5\left \lfloor\frac{15}{3}\right\rfloor = 5 burles.
  • In the second package put the first and fifth goods, the weight is a1+a5=3+4=7a_1 + a_5 = 3 + 4 = 7. The cost of the package is ⌊73⌋=2\left \lfloor\frac{7}{3}\right\rfloor = 2 burles.
  • In the third package put the second and fourth goods, the weight is a2+a4=2+1=3a_2 + a_4 = 2 + 1 = 3. The cost of the package is ⌊33⌋=1\left \lfloor\frac{3}{3}\right\rfloor = 1 burle.

With this packing, the total cost of all packs would be 5+2+1=85 + 2 + 1 = 8 burles.

一批 nn 件商品(nn 为偶数)被运送到商店,其中第 ii 件商品的重量为 aia_i。在销售前,这些商品必须被装入包装盒中。装盒后将执行以下操作:

  • 共形成 n2\frac{n}{2} 个包装盒,每个包装盒中恰好包含两件商品;
  • 包含索引为 ii 和 jj(1≤i,j≤n1 \le i, j \le n)的两件商品的包装盒的重量为 ai+aja_i + a_j。

此时,重量为 xx 的包装盒的售价恒为 ⌊xk⌋\left\lfloor\frac{x}{k}\right\rfloor 卢布(向下取整),其中 kk 是一个给定的固定值。

请将商品两两配对装盒,使得销售总收益最大化。换言之,请构造 n2\frac{n}{2} 对给定商品,使得各包装盒重量 xix_i(1≤i≤n21 \le i \le \frac{n}{2})对应的 ⌊xik⌋\left\lfloor\frac{x_i}{k}\right\rfloor 之和最大。

例如,设 n=6, k=3n = 6,\ k = 3,商品重量为 a=[3,2,7,1,4,8]a = [3, 2, 7, 1, 4, 8]。我们可将它们按如下方式装盒:

  • 第一个包装盒放入第 3 件和第 6 件商品,其重量为 a3+a6=7+8=15a_3 + a_6 = 7 + 8 = 15,售价为 ⌊153⌋=5\left\lfloor\frac{15}{3}\right\rfloor = 5 卢布;
  • 第二个包装盒放入第 1 件和第 5 件商品,其重量为 a1+a5=3+4=7a_1 + a_5 = 3 + 4 = 7,售价为 ⌊73⌋=2\left\lfloor\frac{7}{3}\right\rfloor = 2 卢布;
  • 第三个包装盒放入第 2 件和第 4 件商品,其重量为 a2+a4=2+1=3a_2 + a_4 = 2 + 1 = 3,售价为 ⌊33⌋=1\left\lfloor\frac{3}{3}\right\rfloor = 1 卢布。

采用这种装盒方式,所有包装盒的总售价为 5+2+1=85 + 2 + 1 = 8 卢布。

输入格式

The first line of the input contains an integer tt (1≤t≤1041 \le t \le 10^4) —the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains two integers nn (2≤n≤2⋅1052 \le n \le 2\cdot10^5) and kk (1≤k≤10001 \le k \le 1000). The number nn — is even.

The second line of each test case contains exactly nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \le a_i \le 10^9).

It is guaranteed that the sum of nn over all the test cases does not exceed 2⋅1052\cdot10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——表示测试用例的数量。

接下来是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn(2≤n≤2⋅1052 \le n \le 2\cdot10^5)和 kk(1≤k≤10001 \le k \le 1000)。其中 nn 为偶数。

每个测试用例的第二行包含恰好 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤1090 \le a_i \le 10^9)。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, print on a separate line a single number — the maximum possible total cost of all the packages.

对于每个测试用例,在单独一行中输出一个整数——所有包裹的总成本的最大可能值。

输入输出样例

  • 输入#1

    6
    6 3
    3 2 7 1 4 8
    4 3
    2 1 5 6
    4 12
    0 0 0 0
    2 1
    1 1
    6 10
    2 0 0 5 9 4
    6 5
    5 3 8 6 3 2

    输出#1

    8
    4
    0
    2
    1
    5

说明/提示

The first test case is analyzed in the statement.

In the second test case, you can get a total value equal to 44 if you put the first and second goods in the first package and the third and fourth goods in the second package.

In the third test case, the cost of each item is 00, so the total cost will also be 00.

第一个测试用例在题目描述中已进行分析。

在第二个测试用例中,若将第一和第二个物品放入第一个包裹,第三和第四个物品放入第二个包裹,则可获得总价值 44。

在第三个测试用例中,每个物品的成本均为 00,因此总成本也为 00。

输入解题思路,AI测评打分。不知道怎么写?

首页