Distance Gym - 102028I(贪心思维)

There are nn points on a horizontal line, labelled with 11 through nn from left to right.

The distance between the ii-th point and the (i+1)(i+1)-th point is aiai.

For each integer kk ranged from 11 to nn, 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 TT indicating the number of test cases which is up to 10001000.

For each test case, the first line contains an integer nn indicating the number of points, where 2≤n≤1052≤n≤105.

The second line contains (n−1)(n−1) positive integers a1,a2,⋯,an−1a1,a2,⋯,an−1, where 1≤ai≤1041≤ai≤104.

We guarantee that the sum of nn in all test cases is up to 106106.

Output
For each test case, output a line containing nn integers, the ii-th of which is the maximum sum of distances in case k=ik=i. You should output exactly one whitespace between every two adjacent numbers and avoid any trailing whitespace in this line.

Example
Input
1
5
2 3 1 4
Output
0 10 20 34 48
Note
The figure below describes the sample test case.
在这里插入图片描述

The only best selection for k=2k=2 should choose the leftmost and the rightmost points, while a possible best selection for k=3k=3 could contain any extra point in the middle.
对于放点,肯定是左边放一个右边放一个这样最终的价值最大。
自己画画图会发现,放右边的时候,和前一个答案的差值等于之前的差值Sum+sum[r]-sum[l]。
放左边的时候,和前一个答案的差值等于之前的差值Sum。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
ll a[maxx],sum[maxx],ans[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<n;i++) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
		int l=0,r=n;
		ll Sum=0;
		for(int i=1;i<=n;i++)
		{
			if(i&1)
			{
				l++;
				ans[i]=ans[i-1]+Sum;
			}
			else 
			{
				r--;
				ans[i]=ans[i-1]+sum[r]-sum[l-1]+Sum;
				Sum=ans[i]-ans[i-1];
			}
		}
		for(int i=1;i<=n;i++) printf("%lld%c",ans[i],i==n?'\n':' ');
	}
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值