解题报告 『[Poetize6]IncDec Sequence(差分)』
差分,想到了这个再看看代码应该就很好做了。
代码实现如下:
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (register int i = (a); i <= (b); i++) #define per(i, a, b) for (register int i = (a); i >= (b); i--) const int maxn = 1e5 + 5; int n, pos = 0, neg = 0; int a[maxn]; int MAX(int a, int b) {return a > b ? a : b;} int ABS(int a, int b) {return a > b ? a - b : b - a;} int read() { int x = 0, flag = 0; char ch = ' '; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') { flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar(); } return flag ? -x : x; } void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } signed main() { n = read(); rep(i, 1, n) a[i] = read(); per(i, n, 2) a[i] -= a[i - 1]; rep(i, 2, n) { if (a[i] > 0) pos += a[i]; if (a[i] < 0) neg -= a[i]; } write(MAX(pos, neg)); printf("\n"); write(ABS(pos, neg) + 1); return 0; }