Prefix and a sequence of changes in the differential ----------

Given a length nn number of rows a1, a2, ..., ana1, a2, ..., an, each can select an interval [l, r], the number of the subscript in this section are incremented or decremented by a .
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 nn.
Next nn lines each enter an integer, the i + 1 row represent an integer aiai.
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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long LL ;
int n;
int a[N], b[N];
int main(){
 scanf("%d",&n);
 for (int i = 1; i <= n; i ++)    scanf("%d", &a[i]);
 for (int i = 1; i <= n; i ++)    b[i] = a[i] - a[i - 1];
 LL p = 0, q = 0;
 for (int i = 2; i <= n; i ++)
  if (b[i] >= 0)    p += b[i];
  else              q -= b[i];
  cout << max(p, q) << endl;
  cout << abs(p - q) + 1 << endl;
    return 0;
} 
Published 106 original articles · won praise 67 · views 5444

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/104687418