HDU 5805 - NanoApe Loves Sequence

54 篇文章 0 订阅
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
 

题意:给出一个队列,随机删除其中的一个数,求出最大相邻两个数的差的绝对值的和的期望。

听起来绕来绕去,其实就是枚举每个元素被删除,然后找出其中最大差值的绝对值,把他们全都加起来就好了。

不过我们要记住差值前三的值,因为删掉的可能是第一和第二共有的那个元素。

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

long long a[100000 + 5];
long long d[100000 + 5];
const long long INF = 2e9;

int main()
{
    int T;
    int n;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i)
            scanf("%I64d", &a[i]);

        long long maxn_1 = -INF, maxn_2 = -INF, maxn_3 = -INF;///初始化,很重要,因为这个WA了好几遍
        int num_1 = 0, num_2 = 0;
        for (int i = 2; i <= n; ++i)
        {
            d[i] = a[i] - a[i-1];
            if (abs(d[i]) > maxn_1)///第一大
            {
                maxn_2 = maxn_1;
                maxn_1 = abs(d[i]);
                num_2 = num_1;
                num_1 = i;
            }
            else if (abs(d[i]) > maxn_2)///第二大
            {
                maxn_2 = abs(d[i]);
                num_2 = i;
            }
        }
        if (n > 3)
        {
            for (int i = 2; i <= n; ++i)///第三大
            {
                if (i != num_1 && i != num_2 && d[i] > maxn_3)
                    maxn_3 = d[i];
            }
        }
        long long ans = 0;
        if (d[2] == maxn_1) ans += maxn_2;///删除第一个或者最后一个时
        else ans += maxn_1;
        if (d[n] == maxn_1) ans += maxn_2;
        else ans += maxn_1;

        for (int i = 2; i < n; ++i)///枚举每个删除点
        {
            long long val = abs(a[i+1] - a[i-1]);
            if (val >= maxn_1) ans += val;
            else
            {
                if ((i == num_1 && i+1 == num_2) || (i == num_2 && i+1 == num_1))
                    ans += max(maxn_3, val);
                else if (i == num_1 || i+1 == num_1)
                    ans += max(maxn_2, val);
                else
                    ans += maxn_1;
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值