差分
作者:
xm03
,
2022-01-23 21:37:39
,
所有人可见
,
阅读 177
100. 增减序列
题解
C++ 代码
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n;
int a[N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for(int i = n; i >= 2; i -- ) a[i] -= a[i - 1];
LL pos = 0, neg = 0; // b2~bn的正数和,负数和的绝对值
for(int i = 2; i <= n ;i ++ )
{
if(a[i] > 0) pos += a[i];
else neg -= a[i];
}
cout << min(pos, neg) + abs(pos - neg) << endl;
cout << abs(pos - neg) + 1 << endl;
return 0;
}