Problem Description
There are nn points on a horizontal line, labelled with 1 through nn from left to right.
The distance between the i-th point and the (i+1)-th point is aiai.
For each integer k ranged from 1 to n, you are asked to select exactly kk different given points on the line to maximize the sum of distances between all pairs of selected points.
Input
The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 1000.
For each test case, the first line contains an integer nn indicating the number of points, where 2≤n≤105.
The second line contains (n−1) positive integers a1,a2,⋯,an−1, where 1≤ai≤1041≤ai≤104.
We guarantee that the sum of nn in all test cases is up to 106.
Output
For each test case, output a line containing nn integers, the i-th of which is the maximum sum of distances in case k=i. You should output exactly one whitespace between every two adjacent numbers and avoid any trailing whitespace in this line.
Examples
Input
1
5
2 3 1 4Output
0 10 20 34 48
Note
The figure below describes the sample test case.
The only best selection for k=2 should choose the leftmost and the rightmost points, while a possible best selection for k=3 could contain any extra point in the middle.
题意:t 组样例,给出 n 个点,以及每两个点之间的距离,分别选 1~n 个点,使得点之间距离最大,输出距离和
思路:
由于每次选一个点,要比原来的距离再加上这个新点与其他点的距离,即加上上一次点的左右边界,因此为了使加的最大,就需要让每一次的范围尽可能的大,因此每次从最左、最右分别选点即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<LL,LL>
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 1000050+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
LL a[N];
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n;
scanf("%d",&n);
LL sum=0;
for(int i=1;i<=n-1;i++){
scanf("%lld",&a[i]);
sum+=a[i];
}
LL cnt=0;
LL res=0;
int left=1,right=n-1;
for(int i=1;i<=n;i++){
if(i%2==0){
cnt+=sum;
sum-=a[left];
sum-=a[right];
left++;
right--;
}
res+=cnt;
if(i!=n)
printf("%lld ",res);
else
printf("%lld\n",res);
}
}
return 0;
}