P4552 [Poetize6] IncDec Sequence Difference

The subject is not difficult, but ingenious idea.

We seek to travel the score group, it becomes a three operations

1. A number plus a
2 a a reduced number
3. A number plus one, while a number minus one

We want \ (2 \) ~ \ (n-\) becomes \ (0 \) , and the set of all integers is \ (S1 \) , and the absolute value of negative \ (S2 \) , then the minimum operation number \ (max (s1, s2) \)

We \ (min (s1, s2) \) times let \ (s1, s2 \) in an becomes \ (0 \) , the remaining \ (max (s1, s2) - min (s1, s2) \) times can be a value obtained by adding the position of operation start, a total of \ (max (s1, s2) - min (s1, s2) + 1 \) optional values.

#include<iostream>
#include<cstdio>
using namespace std;
int n, ans;
long long sum1, sum2;
const int N = 100010;
int a[N];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)scanf("%d", &a[i]);
	for (int i = n; i >= 1; --i)a[i] -= a[i - 1];
	for (int i = 2; i <= n; ++i)
		(a[i] > 0 ? sum1 : sum2) += a[i];
	sum2 = -sum2;
	cout << max(sum1, sum2) << endl << (max(sum1, sum2) - min(sum1, sum2)) + 1;
}

Guess you like

Origin www.cnblogs.com/wljss/p/12601546.html