hdu NanoApe Loves Sequence

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
 



PS:题意:任意去除一个数,找到每次两两差值绝对值的最大值之和

思路:找掐头去尾后差值最大值且记录差值并排序,相加后再找到去除任意一个数后的差值和未去除数差值中最大值比较,选取最大值相加

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[100020],b[100020],p[100020];
int abs(int a)
{
    return a>0?a:-a;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        long long sum=0,maxx=0,num;
        int k=1;
        scanf("%d",&n);
        scanf("%d",&a[0]);
        scanf("%d",&a[1]);
        int w=a[1]-a[0];
           b[0]=w;
           p[0]=w;
        for(int i=2; i<n; i++)//找掐头或去尾后最大值去相加
        {
            scanf("%d",&a[i]);

            b[k]=abs(a[i]-a[i-1]);
            p[k]=b[k];
            if(i==n-1)
            {
                if(maxx<b[k])
                {
                    maxx=b[k];
                }
                if(w>maxx)
                {
                    sum=maxx;
                    sum+=w;
                    maxx=b[k];
                }
                else
                {
                    if(maxx<b[k])
                    {
                        sum=maxx;
                        sum+=b[k];
                        maxx=b[k];
                    }
                    else
                    {
                        sum=maxx;
                        sum*=2;
                    }

                }
            }
            else
            {
                if(maxx<b[k])
                {
                    maxx=b[k];
                }
            }
            k++;
        }
        sort(p,p+k);//记录差值
       // printf("%d %d %d %d %lld\n",p[0],p[1],p[2],p[3],sum);
        for(int i=2; i<n; i++)
        {
            num=abs(a[i]-a[i-2]);
            if(abs(a[i-1]-a[i-2])==p[k-1]&&abs(a[i]-a[i-1])!=p[k-2]||abs(a[i-1]-a[i-2])!=p[k-2]&&abs(a[i]-a[i-1])==p[k-1])
            {
                if(num<p[k-2])//若去除的数牵扯到最大值就加第二大值
                    sum+=p[k-2];
                else
                    sum+=num;
            }
            else if(abs(a[i-1]-a[i-2])==p[k-1]&&abs(a[i]-a[i-1])==p[k-2]||abs(a[i-1]-a[i-2])==p[k-2]&&abs(a[i]-a[i-1])==p[k-1])
            {
                if(num<p[k-3])//若去除的数牵扯到最大值和第二大值就加第三大值
                    sum+=p[k-3];
                else
                    sum+=num;
            }
            else
            { if(num<p[k-1])
                    sum+=p[k-1];
                else
                    sum+=num;

            }
            //printf("%lld\n",sum);
        }

        printf("%I64d\n",sum);

    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值