其他分享
首页 > 其他分享> > Divan and a New Project

Divan and a New Project

作者:互联网

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

Let xi be the coordinate of the i-th building. To get from the building i to the building j, Divan spends |xi−xj| 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 ai times, each time spending 2⋅|x0−xi| 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.

Input

Each test contains several test cases. The first line contains one integer number t (1≤t≤103) — the number of test cases.

The first line of each case contains an integer n (1≤n≤2⋅105) — the number of buildings that "Divan's Sofas" is going to build, apart from the headquarters.

The second line contains the sequence a1,a2,…,an (0≤ai≤106), where ai 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⋅105.

Output

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 x0,x1,…,xn of n+1 integers, where xi (−106≤xi≤106) is the selected coordinate of the i-th building. It can be shown that an optimal answer exists with coordinates not exceeding 106.

If there are multiple answers, print any of them.

Example

input

Copy

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

output

Copy

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

Note

Let's look at the first example.

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

  1. the headquarters is located in x0=2;
  2. x1=4: Divan will spend 2⋅|x0−x1|⋅a1=2⋅|2−4|⋅1=4 minutes walking to the first building;
  3. x2=1: Divan will spend 2⋅|x0−x2|⋅a2=2⋅|2−1|⋅2=4 minutes walking to the second building;
  4. x3=3: Divan will spend 2⋅|x0−x3|⋅a3=2⋅|2−3|⋅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.

思路:这个题的思路很明显,就是要你由大到小排一下序,然后再按原来的顺序输出出来,这里说明最少有两个变量,一个是值,另一个是i(下标)。(个人感觉这可比vector<pair<int,int>>v(n)要好用的多)

这里用一下结构体的小技巧:让结构体里一个记录值,一个记录i,然后以值的大小排序让值更新为我们所需要的,最后用i由小到大输出。在排序时,定义cmp时,要定义为bool类型,而且return时,不能带等号!参数类型为node类型。return a.first>b.first 就是由大到小排序。

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=2e5+10;

struct node
{
    ll a;
    ll id;
}nod[N];

bool cmp1(node x,node y)//jiangxu  a[i]
{
    return x.a>y.a;
}

bool cmp2(node x,node y)// id
{
    return x.id<y.id;
}

int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--)
    {

        ll n;
        scanf("%lld",&n);

        for(int i=0;i<n;i++)
        {
            scanf("%lld",&nod[i].a);
            nod[i].id=i;
        }

        sort(nod,nod+n,cmp1);
        ll d=0;
        ll sum=0;
        ll kk;
        for(int i=0;i<n;i++)
        {
            kk=nod[i].a;
            if(i%2==0)
            {
                d++;
                nod[i].a=d;

            }
            else
            {
                nod[i].a=-d;
            }
            sum+=kk*d;
        }

        sort(nod,nod+n,cmp2);

        printf("%lld\n",2*sum);
        printf("0 ");
        for(int i=0;i<n;i++)
        {
            printf("%lld ",nod[i].a);
        }printf("\n");
    }
    return 0;
}

标签:building,will,walking,number,Project,Divan,New,first
来源: https://blog.csdn.net/qq_51690312/article/details/121578819