309028: CF1614B. Divan and a New Project

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Divan and a New Project

题意翻译

### 题目描述 有 $n + 1$ 座的建筑,编号从 $0\sim n$。 有一个人从编号为 $0$ 的建筑出发, 分别要去编号为 $i$ 的建筑 $a_i$ 次。 设编号为 $i$ 的建筑坐标为 $x_i$, 这个人往返编号为 $i$ 的建筑一趟花费的时间为 $2 \times|x_i - x_0|$ 。 求如何安排这 $n + 1$ 座建筑的坐标, 使这个人在路上花费的总时间最小。 ### 输入格式 输入第一行一个整数 $t$,表示有 $t$ 组数据。 每组数据第一行一个整数 $n$, 第二行 $n$ 个数, 分别表示 $a_1$ …… $a_n$。 ### 输出格式 每组数据输出共两行,第一行一个整数,表示最小要花费的总时间。第二行 $n + 1$ 个整数, 表示使总时间花费最小的 $x_0$ …… $x_n$。如果有多组符合条件的解,输出任意一组即可。 ### 数据范围与约定 对于 $100\%$ 的数据,$1 \le t \le 10^3$,$1 \le n \le 2 \cdot 10^5$,$\sum n \le 2 \cdot 10^5$,$0 \le a_i \le 10^6$,$-10^6 \le x_i \le 10^6$。

题目描述

The company "Divan's Sofas" is planning to build $ n + 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 $ x_i $ be the coordinate of the $ i $ -th building. To get from the building $ i $ to the building $ j $ , Divan spends $ |x_i - x_j| $ minutes, where $ |y| $ is the absolute value of $ y $ . All buildings that Divan is going to build can be numbered from $ 0 $ to $ n $ . The businessman will live in the building $ 0 $ , the new headquarters of "Divan's Sofas". In the first ten years after construction Divan will visit the $ i $ -th building $ a_i $ times, each time spending $ 2 \cdot |x_0-x_i| $ minutes for walking. Divan asks you to choose the coordinates for all $ n + 1 $ buildings so that over the next ten years the businessman will spend as little time for walking as possible.

输入输出格式

输入格式


Each test contains several test cases. The first line contains one integer number $ t $ ( $ 1 \le t \le 10^3 $ ) — the number of test cases. The first line of each case contains an integer $ n $ ( $ 1 \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 $ a_1, a_2, \ldots, a_n $ ( $ 0 \le a_i \le 10^6 $ ), where $ a_i $ is the number of visits to the $ i $ -th building. It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式


For each test case, on the first line print the number $ T $ — the minimum time Divan will spend walking. On the second line print the sequence $ x_0, x_1, \ldots, x_n $ of $ n + 1 $ integers, where $ x_i $ ( $ -10^6 \le x_i \le 10^6 $ ) is the selected coordinate of the $ i $ -th building. It can be shown that an optimal answer exists with coordinates not exceeding $ 10^6 $ . If there are multiple answers, print any of them.

输入输出样例

输入样例 #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 $ a_1 = 1 $ times, the second $ a_2 = 2 $ times and the third $ a_3 = 3 $ times. Then one of the optimal solution will be as follows: 1. the headquarters is located in $ x_0 = 2 $ ; 2. $ x_1 = 4 $ : Divan will spend $ 2 \cdot |x_0-x_1| \cdot a_1 = 2 \cdot |2-4| \cdot 1 = 4 $ minutes walking to the first building; 3. $ x_2 = 1 $ : Divan will spend $ 2 \cdot |x_0-x_2| \cdot a_2 = 2 \cdot |2-1| \cdot 2 = 4 $ minutes walking to the second building; 4. $ x_3 = 3 $ : Divan will spend $ 2 \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 = 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 = [-5, -3, -6, -4] $ are also correct answers for the first example.

Input

题意翻译

### 题目描述 有 $n + 1$ 座的建筑,编号从 $0\sim n$。 有一个人从编号为 $0$ 的建筑出发, 分别要去编号为 $i$ 的建筑 $a_i$ 次。 设编号为 $i$ 的建筑坐标为 $x_i$, 这个人往返编号为 $i$ 的建筑一趟花费的时间为 $2 \times|x_i - x_0|$ 。 求如何安排这 $n + 1$ 座建筑的坐标, 使这个人在路上花费的总时间最小。 ### 输入格式 输入第一行一个整数 $t$,表示有 $t$ 组数据。 每组数据第一行一个整数 $n$, 第二行 $n$ 个数, 分别表示 $a_1$ …… $a_n$。 ### 输出格式 每组数据输出共两行,第一行一个整数,表示最小要花费的总时间。第二行 $n + 1$ 个整数, 表示使总时间花费最小的 $x_0$ …… $x_n$。如果有多组符合条件的解,输出任意一组即可。 ### 数据范围与约定 对于 $100\%$ 的数据,$1 \le t \le 10^3$,$1 \le n \le 2 \cdot 10^5$,$\sum n \le 2 \cdot 10^5$,$0 \le a_i \le 10^6$,$-10^6 \le x_i \le 10^6$。

加入题单

算法标签: