hdu 5805 NanoApe Loves Sequence(简单思路题)

在准备高考的数学课上,NanoApe遇到了一个有趣的序列问题:从一个序列中随机删除一个数后,求所有相邻两数差的绝对值的最大值的期望。本篇将介绍如何解决这个问题并提供一种有效的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NanoApe Loves Sequence

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


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
 

题意:退役狗 NanoApe 滚回去学文化课啦!
在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 nn 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值。
他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。

思路:先求出整个序列里面相邻两个数差值的最大值跟次大值

然后枚举删除每一个数后最大值会不会有所改变,加上对应的即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 100050
long long INF=2000000001;
long long a[N],d[N];
int main()
{
    int T,n,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%I64d",&a[i]);
        long long maxn=-INF,maxn1=-INF,num,num1,maxn2=-INF;
        for(int i=2; i<=n; i++)
        {
            d[i]=a[i]-a[i-1];
            if(maxn<abs(d[i]))
            {
                maxn1=maxn;
                maxn=abs(d[i]);
                num1=num;
                num=i;
            }
            else if(maxn1<abs(d[i]))
            {
                maxn1=abs(d[i]);
                num1=i;
            }
        }
        if(n>3)
        {
            for(int i=2;i<=n;i++)
                if(i!=num&&i!=num1&&maxn2<d[i])
                maxn2=d[i];
        }
        long long sum=0;
        if(abs(d[2])==maxn) sum+=maxn1;
        else sum+=maxn;
        if(abs(d[n])==maxn) sum+=maxn1;
        else sum+=maxn;
        for(int i=2; i<n; i++)
        {
            long long l=abs(a[i-1]-a[i+1]);
            if(l>=maxn) sum+=l;
            else
            {
                if((num==i&&num1==i+1)||(num1==i+1&&num==i))
                    sum+=max(l,maxn2);
                else if(num==i||num==i+1)
                    sum+=max(l,maxn1);
                else
                    sum+=maxn;
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值