增减序列

[https://www.acwing.com/problem/content/description/102/]

sol:
区间加减首先想到差分,差分可以将区间操作转化为单点操作。设差分数组为 \(b\),那么题目就转化为:

1.求将 \(b_2\) ~ \(b_n\)都变为\(0\)的最小操作次数。
2.再最小操作次数下, \(b_1\) 有多少种取值。
接下来就非常简单了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

const int N = 1e5 + 100;
typedef long long LL;

LL n, a[N], b[N];
LL sum1, sum2;

int main()
{
	scanf("%lld", &n);
	for(int i = 1; i <= n; i ++) {
		scanf("%lld", &a[i]);
		if( i == 1)    continue;
		b[i] = a[i] - a[i - 1];
		if( b[i] > 0)
			sum1 += b[i];
		else 
			sum2 -= b[i]; 
	}
	printf("%lld\n", max(sum1, sum2));
	printf("%lld\n", abs(sum1 - sum2) + 1ll);
	return 0;
} 
posted @ 2020-09-09 19:27  王雨阳  阅读(132)  评论(0编辑  收藏  举报