codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1614B.md (3088B)


      1 Source: https://codeforces.com/problemset/problem/1614/B
      2 
      3 B. Divan and a New Project
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The company "Divan's Sofas" is planning to build $n + 1$ different buildings on a coordinate line so that:
     22 
     23 * the coordinate of each building is an integer number;
     24 * no two buildings stand at the same point.
     25 
     26 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$.
     27 
     28 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.
     29 
     30 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.
     31 
     32 Input
     33 
     34 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.
     35 
     36 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.
     37 
     38 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.
     39 
     40 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     41 
     42 Output
     43 
     44 For each test case, on the first line print the number $T$ — the minimum time Divan will spend walking.
     45 
     46 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$.
     47 
     48 If there are multiple answers, print any of them.
     49 
     50 Example
     51 
     52 Input
     53 
     54 ```
     55 4
     56 3
     57 1 2 3
     58 5
     59 3 8 10 6 1
     60 5
     61 1 1 1 1 1
     62 1
     63 0
     64 ```
     65 
     66 Output
     67 
     68 ```
     69 14
     70 2 4 1 3
     71 78
     72 1 -1 0 2 3 4
     73 18
     74 3 6 1 5 2 4
     75 0
     76 1 2
     77 ```
     78 
     79 Note
     80 
     81 Let's look at the first example.
     82 
     83 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:
     84 
     85 1. the headquarters is located in $x_0 = 2$;
     86 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;
     87 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;
     88 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.
     89 
     90 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.
     91 
     92 Among others, $x = [1, 3, 2, 0]$, $x = [-5, -3, -6, -4]$ are also correct answers for the first example.