知识点:差分
一道标准的差分题,用这道题来学习一维差分的基本知识与操作,至于思路,李煜东讲的很清楚了,再补充一点,一个序列,如果它是相等的,那么它的差分序列,第一个数就是这个序列的大小,从2到n都是0,这是相等序列的差分序列的特征,后面还遇到了差分套差分,那个就有点难想了,
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
const int N = 1e6 + 5;
ll a[N], s[N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s[i] = a[i] - a[i - 1];
}
ll t1 = 0, t2 = 0;
for (int i = 2; i <= n; i++) {
if (s[i] > 0) t1 += s[i];
if (s[i] < 0) t2 -= s[i];
}
ll x = min(t1, t2), y = abs(t1 - t2);
cout << x + y << endl << y + 1;
return 0;
}