CH0304 IncDec Sequence (差分)

题目链接:
https://ac.nowcoder.com/acm/contest/999/B

思路:(见图中解释)

image-20200807163415329

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100006;
ll a[N], b[N];
int main() {
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i)cin >> a[i];
    for (int i = 2; i <= n; ++i)b[i] = a[i] - a[i - 1];
    ll p = 0, q = 0;
    for (int i = 2; i <= n; i++)
        if (b[i] > 0) p += b[i];
        else if (b[i] < 0) q -= b[i];
    cout << max(p, q) << endl << abs(p - q) + 1 << endl;
}
posted @ 2020-08-07 16:36  RioTian  阅读(125)  评论(0)    收藏  举报