CF1614B.Divan and a New Project

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The company "Divan's Sofas" is planning to build n+1n + 1 different buildings on a coordinate line so that:

  • the coordinate of each building is an integer number;
  • no two buildings stand at the same point.

Let xix_i be the coordinate of the ii-th building. To get from the building ii to the building jj, Divan spends ∣xi−xj∣|x_i - x_j| minutes, where ∣y∣|y| is the absolute value of yy.

All buildings that Divan is going to build can be numbered from 00 to nn. The businessman will live in the building 00, the new headquarters of "Divan's Sofas". In the first ten years after construction Divan will visit the ii-th building aia_i times, each time spending 2⋅∣x0−xi∣2 \cdot |x_0-x_i| minutes for walking.

Divan asks you to choose the coordinates for all n+1n + 1 buildings so that over the next ten years the businessman will spend as little time for walking as possible.

公司“Divan’s Sofas”计划在一条数轴上建造 n+1n + 1 座互不相同的建筑,满足以下条件:

  • 每座建筑的坐标均为整数;
  • 任意两座建筑不能位于同一点。

设第 ii 座建筑的坐标为 xix_i。从建筑 ii 到建筑 jj,Divan 需花费 ∣xi−xj∣|x_i - x_j| 分钟,其中 ∣y∣|y| 表示 yy 的绝对值。

Divan 计划建造的所有建筑编号为 00 至 nn。商人将居住在编号为 00 的建筑中,即“Divan’s Sofas”的新总部。在建成后最初的十年间,Divan 将访问第 ii 座建筑 aia_i 次,每次往返均需步行 2⋅∣x0−xi∣2 \cdot |x_0 - x_i| 分钟。

Divan 请你为全部 n+1n + 1 座建筑选定坐标,使得未来十年内商人步行所花费的总时间最少。

输入格式

Each test contains several test cases. The first line contains one integer number tt (1≤t≤1031 \le t \le 10^3) — the number of test cases.

The first line of each case contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of buildings that "Divan's Sofas" is going to build, apart from the headquarters.

The second line contains the sequence a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤1060 \le a_i \le 10^6), where aia_i is the number of visits to the ii-th building.

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

每个测试包含若干测试用例。第一行包含一个整数 tt(1≤t≤1031 \le t \le 10^3),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示“Divan's Sofas”公司除总部外计划建造的楼栋数量。

第二行包含一个序列 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤1060 \le a_i \le 10^6),其中 aia_i 表示第 ii 栋楼的访问次数。

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

输出格式

For each test case, on the first line print the number TT — the minimum time Divan will spend walking.

On the second line print the sequence x0,x1,…,xnx_0, x_1, \ldots, x_n of n+1n + 1 integers, where xix_i (−106≤xi≤106-10^6 \le x_i \le 10^6) is the selected coordinate of the ii-th building. It can be shown that an optimal answer exists with coordinates not exceeding 10610^6.

If there are multiple answers, print any of them.

对于每个测试用例,在第一行输出数字 TT —— Divan 行走所需的最少时间。

在第二行输出由 n+1n + 1 个整数组成的序列 x0,x1,…,xnx_0, x_1, \ldots, x_n,其中 xix_i(−106≤xi≤106-10^6 \le x_i \le 10^6)表示第 ii 座建筑物所选定的坐标。可以证明:存在一个最优解,其坐标值均不超过 10610^6。

若存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    3
    1 2 3
    5
    3 8 10 6 1
    5
    1 1 1 1 1
    1
    0

    输出#1

    14
    2 4 1 3
    78
    1 -1 0 2 3 4
    18
    3 6 1 5 2 4
    0
    1 2

说明/提示

Let's look at the first example.

Divan will visit the first building a1=1a_1 = 1 times, the second a2=2a_2 = 2 times and the third a3=3a_3 = 3 times. Then one of the optimal solution will be as follows:

  1. the headquarters is located in x0=2x_0 = 2;
  2. x1=4x_1 = 4: Divan will spend 2⋅∣x0−x1∣⋅a1=2⋅∣2−4∣⋅1=42 \cdot |x_0-x_1| \cdot a_1 = 2 \cdot |2-4| \cdot 1 = 4 minutes walking to the first building;
  3. x2=1x_2 = 1: Divan will spend 2⋅∣x0−x2∣⋅a2=2⋅∣2−1∣⋅2=42 \cdot |x_0-x_2| \cdot a_2 = 2 \cdot |2-1| \cdot 2 = 4 minutes walking to the second building;
  4. x3=3x_3 = 3: Divan will spend 2⋅∣x0−x3∣⋅a3=2⋅∣2−3∣⋅3=62 \cdot |x_0-x_3| \cdot a_3 = 2 \cdot |2-3| \cdot 3 = 6 minutes walking to the third building.

In total, Divan will spend 4+4+6=144 + 4 + 6 = 14 minutes. It can be shown that it is impossible to arrange buildings so that the businessman spends less time.

Among others, x=[1,3,2,0]x = [1, 3, 2, 0], x=[−5,−3,−6,−4]x = [-5, -3, -6, -4] are also correct answers for the first example.

我们来看第一个例子。

Divan 将分别访问第一栋楼 a1=1a_1 = 1 次、第二栋楼 a2=2a_2 = 2 次、第三栋楼 a3=3a_3 = 3 次。此时,一种最优方案如下:

  1. 总部设在 x0=2x_0 = 2;
  2. x1=4x_1 = 4:Divan 前往第一栋楼需花费 2⋅∣x0−x1∣⋅a1=2⋅∣2−4∣⋅1=42 \cdot |x_0-x_1| \cdot a_1 = 2 \cdot |2-4| \cdot 1 = 4 分钟;
  3. x2=1x_2 = 1:Divan 前往第二栋楼需花费 2⋅∣x0−x2∣⋅a2=2⋅∣2−1∣⋅2=42 \cdot |x_0-x_2| \cdot a_2 = 2 \cdot |2-1| \cdot 2 = 4 分钟;
  4. x3=3x_3 = 3:Divan 前往第三栋楼需花费 2⋅∣x0−x3∣⋅a3=2⋅∣2−3∣⋅3=62 \cdot |x_0-x_3| \cdot a_3 = 2 \cdot |2-3| \cdot 3 = 6 分钟。

总计,Divan 将花费 4+4+6=144 + 4 + 6 = 14 分钟。可以证明,不存在其他建筑位置安排方式能使该商人花费更少时间。

此外,对于第一个例子,x=[1,3,2,0]x = [1, 3, 2, 0]、x=[−5,−3,−6,−4]x = [-5, -3, -6, -4] 等也是正确的答案。

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

首页