Codeforces Global Round 7 B - Maximums(思维题)

题意:

有数组 an,定义 xi 为 a 的前 i - 1 位的最大值(x= 0),定义 bi 为 ai - xi,给出数组 bn,还原数组 an

思路:

因为 x= 0,所以 b0 = a0,之后维护最大值即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int n;cin>>n;
    ll b[n];for(ll &i:b) cin>>i;
    ll a[n]={b[0]};
    ll mx=b[0];
    for(int i=1;i<n;i++){
        a[i]=mx+b[i];
        if(b[i]>0) mx+=b[i];
    }
    for(int i:a) cout<<i<<' ';
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Kanoon/p/12528923.html
今日推荐