1. 程式人生 > >hdu 3434 給你含有n個數的序列,每次你可以選一個子序列將上面所有的數字加1或者減1,目標是把所有數字變成相同的,問最少步數,和那個相同的數字有多少種可能

hdu 3434 給你含有n個數的序列,每次你可以選一個子序列將上面所有的數字加1或者減1,目標是把所有數字變成相同的,問最少步數,和那個相同的數字有多少種可能

Problem Description Given a sequence consists of N integers. Each time you can choose a continuous subsequence and add 1 or minus 1 to the numbers in the subsequence .You task is to make all the numbers the same with 
the least tries. You should calculate the number of the least tries 
you needed and the number of different final sequences with the least tries.
Input In the first line there is an integer T, indicates the number of test cases.(T<=30)
In each case, the first line contain one integer N(1<=N<=10^6), 
the second line contain N integers and each integer in the sequence is between [1,10^9].
There may be some blank lines between each case.

Output For each test case , output “Case d: x y “ where d is the case number 
counted from one, x is the number of the least tries you need and y 
is the number of different final sequences with the least tries.
Sample Input 2 2 2 4 6 1 1 1 2 2 2
Sample Output Case 1: 2 3 Case 2: 1 2 Hint
In sample 1, we can add 1 twice at index 1 to get {4,4},or minus 1 twice at index 2 to get {2,2}, or we can add 1 once at index 1 and minus 1 once at index 2 to get {3,3}. So there are three different final sequences.

//

//此題先要確定最優值範圍在a[0]與a[n]之間所有值,所以有Max-Min+1個,任取一個最優值,第一個 //這樣,可以得到最大上升偏移量,和最大下降偏移量,去其最大的就是最少要移動的次數 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int A[1000005];
int main() {
    int cas,r=1;
    scanf("%d",&cas);
    while(cas--){
        int N;
        scanf("%d",&N);
        for(int i=0;i<N;i++)
            scanf("%d",&A[i]);
        long long a=0,b=0;
        for(int i=1;i<N;i++)
        {
            if (A[i]-A[i-1]>0) a+=A[i]-A[i-1];
            else b-=A[i]-A[i-1];
        }
        int Max = max(A[0],A[N-1]), Min = min(A[0],A[N-1]);
        printf("Case %d: %I64d %I64d\n",r++,(a>b?a:b),Max-Min+1);
    }
}