Codeforces--1326B--Maximums

229 篇文章 0 订阅
14 篇文章 0 订阅

题目描述:
Alicia has an array, a1,a2,…,an, of non-negative integers. For each 1≤i≤n, she has found a non-negative integer xi=max(0,a1,…,ai−1). Note that for i=1, xi=0.

For example, if Alicia had the array a={0,1,2,0,3}, then x={0,0,1,2,2}.

Then, she calculated an array, b1,b2,…,bn: bi=ai−xi.

For example, if Alicia had the array a={0,1,2,0,3}, b={0−0,1−0,2−1,0−2,3−2}={0,1,1,−2,1}.

Alicia gives you the values b1,b2,…,bn and asks you to restore the values a1,a2,…,an. Can you help her solve the problem?
输入描述:
The first line contains one integer n (3≤n≤200000) – the number of elements in Alicia’s array.

The next line contains n integers, b1,b2,…,bn (−109≤bi≤109).

It is guaranteed that for the given array b there is a solution a1,a2,…,an, for all elements of which the following is true: 0≤ai≤109.
输出描述:
Print n integers, a1,a2,…,an (0≤ai≤109), such that if you calculate x according to the statement, b1 will be equal to a1−x1, b2 will be equal to a2−x2, …, and bn will be equal to an−xn.

It is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique.
输入:
5
0 1 1 -2 1
3
1000 999999000 -1000000000
5
2 1 2 2 3
输出:
0 1 2 0 3
1000 1000000000 0
2 3 5 7 10
题意:
公式推导
题解
公式推导
代码:

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

const int maxn = 200000 + 5;
int a[maxn],b[maxn];

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        memset(a,0,sizeof(a));
        for(int i = 1; i <= n; i ++) scanf("%d",&a[i]);
        int x = 0;
        b[1] = a[1];
        for(int i = 2; i <= n; i ++){
            //cout<<x<<" "<<a[i]<<endl;
            x = max(x,b[i - 1]);
            b[i] = x + a[i];
        }
        for(int i = 1; i < n; i ++) printf("%d ",b[i]);
        printf("%d\n",b[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值