Distance

题目:

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

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

For each integer k ranged from 1 to n, you are asked to select exactly k 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 n indicating the number of points, where 2≤n≤105.

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

We guarantee that the sum of n in all test cases is up to 106.

Output:

For each test case, output a line containing n 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.

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=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.

题目大意 :

给你n个点之间的距离(n - 1),依次选择 1,2,3,…,n个点,问所选的距离和最大,输出每次的距离和

思路:

第一个无论选哪个点都是0;第二个点要与第一个点距离尽可能的远,所以前俩个点肯定是选择两端的点;第三个点无论选择哪里都在两端之间;第四个点也是在两段之间,但是要与第三个点尽可能的远,所以 三和四也是尽可能的往两边取;剩下情况相同…
所以每次选择左右的点
如图:若有8个点,俩点之间距离用 【 x , y 】来表示,发现规律
先累加每个点之间的距离,任意两点之间的距离【a , b】可以用【0,b】-【0 , a】表示

在这里插入图片描述

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const ll N=1e5+5;
ll a[N];

inline ll read(){
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

int main(){
    ll t,n;
    t=read();
    while(t--){
        n=read();
        for(ll i=1;i<=n-1;i++){
            a[i]=read();
            a[i]+=a[i-1];
        }
        ll sum=0;
        ll k=0;
        ll ans=0;

        for(ll i=0;i<n;i++){
            if(i&1){
                sum+=a[n-1-k]-a[k];
                k++;
                ans+=sum;
            }else{
                ans+=sum;
            }
            if(i!=0) printf(" %lld",ans);
            else printf("%lld",ans);
        }
        printf("\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值