欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AcWing 100. IncDec序列(差分)

程序员文章站 2022-07-12 17:50:58
...

传送门

#include <bits/stdc++.h>
#define int long long

using namespace std;
const int MAXN = 1e5 + 10;
int a[MAXN], n, p, q;

signed main() {
    //freopen("in", "r", stdin);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 2;i <= n; i++){
        if (a[i] - a[i - 1] > 0)
            p += a[i] - a[i - 1];
        else
            q -= a[i] - a[i - 1];
    }
    cout << max(p, q) << endl << abs(p - q) + 1 << endl;
    return 0;
}