A15096 | Price Maximization
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
A batch of $n$ goods ( $n$ — an even number) is brought to the store, $i$ -th of which has weight $a_i$ . Before selling the goods, they must be packed into packages. After packing, the following will be done:
- There will be $\frac{n}{2}$ packages, each package contains exactly two goods;
- The weight of the package that contains goods with indices $i$ and $j$ ( $1 \le i, j \le n$ ) is $a_i + a_j$ .
With this, the cost of a package of weight $x$ is always $\left \lfloor\frac{x}{k}\right\rfloor$ burles (rounded down), where $k$ — 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 $\frac{n}{2}$ pairs of given goods that the sum of the values $\left \lfloor\frac{x_i}{k} \right \rfloor$ , where $x_i$ is the weight of the package number $i$ ( $1 \le i \le \frac{n}{2}$ ), is maximal.
For example, let $n = 6, k = 3$ , weights of goods $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 $a_3 + a_6 = 7 + 8 = 15$ . The cost of the package will be $\left \lfloor\frac{15}{3}\right\rfloor = 5$ burles.
- In the second package put the first and fifth goods, the weight is $a_1 + a_5 = 3 + 4 = 7$ . The cost of the package is $\left \lfloor\frac{7}{3}\right\rfloor = 2$ burles.
- In the third package put the second and fourth goods, the weight is $a_2 + a_4 = 2 + 1 = 3$ . The cost of the package is $\left \lfloor\frac{3}{3}\right\rfloor = 1$ burle.
With this packing, the total cost of all packs would be $5 + 2 + 1 = 8$ burles.
- There will be $\frac{n}{2}$ packages, each package contains exactly two goods;
- The weight of the package that contains goods with indices $i$ and $j$ ( $1 \le i, j \le n$ ) is $a_i + a_j$ .
With this, the cost of a package of weight $x$ is always $\left \lfloor\frac{x}{k}\right\rfloor$ burles (rounded down), where $k$ — 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 $\frac{n}{2}$ pairs of given goods that the sum of the values $\left \lfloor\frac{x_i}{k} \right \rfloor$ , where $x_i$ is the weight of the package number $i$ ( $1 \le i \le \frac{n}{2}$ ), is maximal.
For example, let $n = 6, k = 3$ , weights of goods $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 $a_3 + a_6 = 7 + 8 = 15$ . The cost of the package will be $\left \lfloor\frac{15}{3}\right\rfloor = 5$ burles.
- In the second package put the first and fifth goods, the weight is $a_1 + a_5 = 3 + 4 = 7$ . The cost of the package is $\left \lfloor\frac{7}{3}\right\rfloor = 2$ burles.
- In the third package put the second and fourth goods, the weight is $a_2 + a_4 = 2 + 1 = 3$ . The cost of the package is $\left \lfloor\frac{3}{3}\right\rfloor = 1$ burle.
With this packing, the total cost of all packs would be $5 + 2 + 1 = 8$ burles.
输入格式
The first line of the input contains an integer $t$ ( $1 \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 $n$ ( $2 \le n \le 2\cdot10^5$ ) and $k$ ( $1 \le k \le 1000$ ). The number $n$ — is even.
The second line of each test case contains exactly $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_i \le 10^9$ ).
It is guaranteed that the sum of $n$ over all the test cases does not exceed $2\cdot10^5$ .
The descriptions of the test cases follow.
The first line of each test case contains two integers $n$ ( $2 \le n \le 2\cdot10^5$ ) and $k$ ( $1 \le k \le 1000$ ). The number $n$ — is even.
The second line of each test case contains exactly $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_i \le 10^9$ ).
It is guaranteed that the sum of $n$ over all the test cases does not exceed $2\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 $4$ 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 $0$ , so the total cost will also be $0$ .
In the second test case, you can get a total value equal to $4$ 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 $0$ , so the total cost will also be $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted