Differential Greedy +: IncDec sequence

The original title

Title Description
Given a length n number of rows a1, a2, ..., ana1, a2, ..., an, each can select an interval [l, r] [l, r], the number of index within this range We are incremented or decremented by one.

How many operations need to find at least a count of all columns are the same, and obtained under the premise to ensure that the minimum number, the resulting number of columns may be how many.

Input format
first line of input positive integer n.

Next n lines, each line of input integer, the i + 1 row represent integers ai.

Output format
of the first line of output minimum number of operations.

How many second line output the final result can be obtained.

Data range
0 <n≤1050 <n≤105
0≤ai <21474836480≤ai <2147483648
Input Sample:

. 4
. 1
. 1
2
2
Output Sample:

1
2

Ideas: First, a focus on this problem, that is, the interval [l, r] [l, r] modification operations. This question is because there is a purpose to modify the operating characteristics, that is only a plus or minus one only, rather than + x + x, nor is -x-x, so that we do not need to use advanced data structures, and tree line Fenwick tree, but only need to use differential can be.
Differential: the differential is defined, you can see "algorithm contest Advanced Guide" P21 page.
No difference between the first edition, said here about the definition: For a given number of columns A, it is defined as the difference score column B, B [1] = A [ 1], B [i] = Ai-Ai-1 ( 2 <= i <= n) B [1] = a [1], B [i] = Ai-Ai-1 (2 <= i <= n)
where only said properties, i.e. the interval sequence a [ L, R] [L, R ] plus d, i.e. the Al, Al + 1 .... ArAl, Al + 1 .... Ar are coupled d, it is actually the difference in sequence B, Bl + d, Br + 1-dBl + d, Br + 1-d, all other positions are not changed.
This question is so, we can use this very useful property, because we only require A sequence in all the same number, and do not care about these programs specifically what it is, so that we can transform the subject, that is, will a sequence of + 1, -1 + 1, 1 operation, so that the same sequence a, into the target B2, ..., BnB2, ..., Bn can become all 0, i.e. a sequence are all equal. And the resulting sequence, which is the n B1B1
greedy: because we have said above properties, then we can select every BjBi and Bi and Bj, 2 <= i, j <= n2 <= i, j <= n, and these two numbers, a positive, a negative, positive or negative if the pairing as to why, since we are to the B sequence to be 2 ~ n is 0, so this increase in negative, positive reduction, it is the fastest way to reach the goal of zero state.
For those who can not count BkBk pairing can choose B1B1 or BnBn, these two numbers does not affect, be modified.
So we come to the question head answer is minimum operand min (p, q) + abs (p-q) = max (p, q) min (p, q) + abs (p-q) = max (p, q), then there may be a final sequence of abs (p-q) + 1abs (p-q) +1 case. p is a number of sequences of CKS and b, q is the negative and the sequence b

Author: Qinhuai shore lights dim
link: https: //www.acwing.com/solution/acwing/content/816/
Source: AcWing

Code: • To open long long! ! ! ! (Do not ask me why how say)

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
long long a[N],b[N],n;
int main(){
	std::ios::sync_with_stdio(false);
	cin>>n;
	long long zans=0,fans=0,ans1,ans2;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)b[i]=a[i]-a[i-1];
	for(int i=2;i<=n;i++)
	if(b[i]>0)zans+=b[i];
	else fans+=b[i];
	ans2=abs(fans+zans);
	fans=abs(fans);
	ans1=max(fans,zans);
	cout<<ans1<<endl<<ans2+1; 
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/myhnb/p/11248066.html