HDU 5805 NanoApe Loves Sequence

NanoApe Loves Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 421    Accepted Submission(s): 195


Problem Description
NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination!

In math class, NanoApe picked up sequences once again. He wrote down a sequence with n numbers on the paper and then randomly deleted a number in the sequence. After that, he calculated the maximum absolute value of the difference of each two adjacent remained numbers, denoted as F.

Now he wants to know the expected value of F, if he deleted each number with equal probability.
 

 

Input
The first line of the input contains an integer T, denoting the number of test cases.

In each test case, the first line of the input contains an integer n, denoting the length of the original sequence.

The second line of the input contains n integers A1,A2,...,An, denoting the elements of the sequence.

1T10, 3n100000, 1Ai109
 

 

Output
For each test case, print a line with one integer, denoting the answer.

In order to prevent using float number, you should print the answer multiplied by n.
 

 

Sample Input
1
4
1 2 3 4
 
Sample Output
6
 维护前边差最大值,后边差的最大值。然后枚举删除的数。时间复杂度O(n)
注意abs   zz
/* ***********************************************
Author        :guanjun
Created Time  :2016/8/7 9:01:43
File Name     :hdu5805.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
ll a[100010];
ll p[100100];
ll s[100100];
int n;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        ll ans=0;
        for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
        cle(p);
        for(int i=2;i<=n;i++){
            p[i]=max(abs(a[i]-a[i-1]),p[i-1]);
        }
        cle(s);
        for(int i=n-1;i>=1;i--){
            s[i]=max(s[i+1],abs(a[i+1]-a[i]));
        }
        for(int i=1;i<=n;i++){
            if(i==1)ans+=s[2];
            if(i==n)ans+=p[n-1];
            if(i<n&&i>1){
                ans+=max(max(abs(a[i+1]-a[i-1]),p[i-1]),s[i+1]);
            }
        }
        printf("%I64d\n",ans);

    }
    return 0;
}

 

posted on 2016-08-07 09:57  Beserious  阅读(367)  评论(0编辑  收藏  举报