#P4552. Equalizing Sequence with Range Operations
Equalizing Sequence with Range Operations
Equalizing Sequence with Range Operations
Given an integer sequence \(a_1, a_2, \ldots, a_n\), you are allowed to perform the following operation any number of times:
Choose any contiguous interval \([l, r]\) (where \(1 \le l \le r \le n\)) and either add \(1\) or subtract \(1\) from each element in the interval.
Your task is to determine:
- The minimum number of operations required to make all elements of the sequence equal.
- Under the constraint of using the minimum number of operations, the number of distinct final uniform values that can be obtained.
It can be shown that the minimum number of operations is \[ \frac{|a_n - a_1| + \sum_{i=1}^{n-1} |a_{i+1} - a_i|}{2}, \] where \(|a_n - a_1|\) is the absolute difference between the first and last elements, and \(\sum_{i=1}^{n-1} |a_{i+1} - a_i|\) is the sum of absolute differences between consecutive elements.
The number of distinct final uniform values is given by \[ \max(a_1, a_n) - \min(a_1, a_n) + 1. \]
inputFormat
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)), the length of the sequence. The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(|a_i| \le 10^9\)) representing the sequence.
outputFormat
Output two integers separated by a space: the minimum number of operations needed, and the number of distinct final uniform values that can be reached using these operations.
sample
3
1 3 2
2 2