CF1585C.Minimize Distance

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A total of nn depots are located on a number line. Depot ii lies at the point xix_i for 1≤i≤n1 \le i \le n.

You are a salesman with nn bags of goods, attempting to deliver one bag to each of the nn depots. You and the nn bags are initially at the origin 00. You can carry up to kk bags at a time. You must collect the required number of goods from the origin, deliver them to the respective depots, and then return to the origin to collect your next batch of goods.

Calculate the minimum distance you need to cover to deliver all the bags of goods to the depots. You do not have to return to the origin after you have delivered all the bags.

共有 nn 个仓库位于一条数轴上。对于 1≤i≤n1 \le i \le n,第 ii 个仓库位于坐标 xix_i 处。

你是一名推销员,携带 nn 袋货物,需将其中一袋货物送达每个仓库。你与这 nn 袋货物初始时均位于原点 00。你每次最多可携带 kk 袋货物。你必须从原点取走所需数量的货物,将其运送到对应仓库,然后返回原点取下一批货物。

请计算将全部 nn 袋货物送达各仓库所需经过的最短总路程。在完成全部货物的配送后,你无需再返回原点。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10 5001 \le t \le 10\,500). Description of the test cases follows.

The first line of each test case contains two integers nn and kk (1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5).

The second line of each test case contains nn integers x1,x2,…,xnx_1, x_2, \ldots, x_n (−109≤xi≤109-10^9 \le x_i \le 10^9). It is possible that some depots share the same position.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10 5001 \le t \le 10\,500)。随后是测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 x1,x2,…,xnx_1, x_2, \ldots, x_n(−109≤xi≤109-10^9 \le x_i \le 10^9)。某些仓库的位置可能相同。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer denoting the minimum distance you need to cover to deliver all the bags of goods to the depots.

对于每个测试用例,输出一个整数,表示将所有货物袋运送至仓库所需经过的最小距离。

输入输出样例

  • 输入#1

    4
    5 1
    1 2 3 4 5
    9 3
    -5 -10 -15 6 5 8 3 7 4
    5 3
    2 2 3 3 3
    4 2
    1000000000 1000000000 1000000000 1000000000

    输出#1

    25
    41
    7
    3000000000

说明/提示

In the first test case, you can carry only one bag at a time. Thus, the following is a solution sequence that gives a minimum travel distance: 0→2→0→4→0→3→0→1→0→50 \to 2 \to 0 \to 4 \to 0 \to 3 \to 0 \to 1 \to 0 \to 5, where each 00 means you go the origin and grab one bag, and each positive integer means you deliver the bag to a depot at this coordinate, giving a total distance of 2525 units. It must be noted that there are other sequences that give the same distance.

In the second test case, you can follow the following sequence, among multiple such sequences, to travel minimum distance: 0→6→8→7→0→5→4→3→0→(−5)→(−10)→(−15)0 \to 6 \to 8 \to 7 \to 0 \to 5 \to 4 \to 3 \to 0 \to (-5) \to (-10) \to (-15), with distance 4141. It can be shown that 4141 is the optimal distance for this test case.

在第一个测试用例中,你每次只能携带一个包裹。因此,以下是一个能实现最小旅行距离的解序列:0→2→0→4→0→3→0→1→0→50 \to 2 \to 0 \to 4 \to 0 \to 3 \to 0 \to 1 \to 0 \to 5,其中每个 00 表示你返回原点并取走一个包裹,每个正整数表示你将该包裹送达位于该坐标的仓库,总距离为 2525 单位。需注意,存在其他同样能达到该距离的序列。

在第二个测试用例中,你可在多种可行序列中选择如下序列以实现最小旅行距离:0→6→8→7→0→5→4→3→0→(−5)→(−10)→(−15)0 \to 6 \to 8 \to 7 \to 0 \to 5 \to 4 \to 3 \to 0 \to (-5) \to (-10) \to (-15),总距离为 4141。可以证明,4141 是该测试用例的最优距离。

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

首页