Best Coder #86 1002 NanoApe Loves Sequence

NanoApe Loves Sequence

Accepts: 531

Submissions: 2481

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 262144/131072 K (Java/Others)

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 nnn 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 FFF.

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

Input

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

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

The second line of the input contains nnn integers A1,A2,...,AnA_1, A_2, ..., A_nA?1??,A?2??,...,A?n??, denoting the elements of the sequence.

1≤T≤10, 3≤n≤100000, 1≤Ai≤1091 \le T \le 10,~3 \le n \le 100000,~1 \le A_i \le 10^91≤T≤10, 3≤n≤100000, 1≤A?i??≤10?9??

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 nnn.

Sample Input

Copy

1
4
1 2 3 4

Sample Output

Copy

6
/*第二次来水BC,暑假最后一场了,这次收获不错,水出两题,虽然第二题不知道什么是线段树,但是还是按照自己想法搞出来了,加油*/#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string.h>
#define N 100010
#define M 100000
using namespace std;
long long  n, dp[N],pd[N],a[N], tep;
long long Max(long long a,long long b,long long c)
{
    long long d=max(a,b);
    long long e=max(b,c);
    return max(d,e);
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        memset(pd,0,sizeof(pd));
        vector <long long > v;
        v.clear();
        v.push_back(0);
        scanf("%lld",&n);
        for(int i = 1; i <= n; i++)
        {

            scanf("%lld",&tep);
            v.push_back(tep);
        }
        v.push_back(0);
        dp[1]=0;
        for(int i = 2; i <= n;i++)
            if(abs(v[i] - v[i - 1])>dp[i-1])
            {
                dp[i]=abs(v[i] - v[i-1]);
            }
            else
                dp[i] = dp[i - 1];

        reverse(v.begin(), v.end());
        pd[1]=0;
        for(int i = 2; i <= n; i++)
            if(abs(v[i] - v[i-1]) > pd[i-1])
                pd[i] = abs(v[i] - v[i-1]);
            else
                pd[i] = pd[i - 1];
        reverse(v.begin(), v.end());
        long long s = 0;
        for(int i=1;i<=n;i++)
        {
            if(i==1)
                s+=pd[n-1];
            else if(i==n)
                s+=dp[n-1];
            else
                s+=Max(dp[i-1],abs(v[i+1] - v[i-1]),pd[n-i]);
        }
        printf("%lld\n",s);
    }
    return 0;
}
时间: 2024-10-12 11:01:58

Best Coder #86 1002 NanoApe Loves Sequence的相关文章

HDU 5806 NanoApe Loves Sequence Ⅱ(尺取+思维)——BestCoder Round #86 1003

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 514    Accepted Submission(s): 248 Problem Description NanoApe, the Retired Dog, has returned back to prepare for f

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): 440    Accepted Submission(s): 205 Problem Description NanoApe, the Retired Dog, has returned back to prepare for the

5806 NanoApe Loves Sequence Ⅱ(尺取法)

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 1585    Accepted Submission(s): 688 Description NanoApe, the Retired Dog, has returned back to prepare for for the

5805 NanoApe Loves Sequence

传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 1323    Accepted Submission(s): 521 Description NanoApe, the Retired Dog, has returned back to prepare for the Natio

NanoApe Loves Sequence Ⅱ(尺取法)

题目链接:NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 339    Accepted Submission(s): 165 Problem Description NanoApe, the Retired Dog, has returned back to prepare for

hdu-5806 NanoApe Loves Sequence Ⅱ(尺取法)

题目链接: NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education Entrance Examinatio

hdu-5805 NanoApe Loves Sequence(线段树+概率期望)

题目链接: NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination! In

hdu NanoApe Loves Sequence

NanoApe Loves Sequence Accepts: 505 Submissions: 1646 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entr

hdu NanoApe Loves Sequence Ⅱ

NanoApe Loves Sequence Ⅱ Accepts: 235 Submissions: 396 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education