hdoj5850 NanoApe Loves Sequence BestCoder Round #86

NanoApe Loves Sequence

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


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 $A_1, A_2, ..., A_n$, denoting the elements of the sequence.

$1 \le T \le 10,~3 \le n \le 100000,~1 \le A_i \le 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 $n$.
 

Sample Input
  
  
1 4 1 2 3 4
 

Sample Output
  
  
6
 

Source


给你一个串q,计算出这个串中任意两个相邻位置的差的绝对值的最大值。然后从这个串中删除一个数,那么 任意两个相邻位置的差的绝对值的最大值也可能改变,题目要求求出差的最大值的数学期望
其实很简单。就是计算删除每一个数这个串中的差的最大值会是多少,首先先算出这个串中每个相邻位置的差的绝对值,然后排序
如果删除这个串第一个位置和最末尾位置的数字的话,对于第一位来说,只要看那它与第二位的差绝对值是否为排序数组中的最大值,如果是的话,就在ans里加上排序数组中的倒二位,否则加上末位
删除最后一位数字同理
如果删除的是中间的数字,其实操作和刚刚的也很像,只是要判断i与i+1位,i与i-1位的差的绝对值在排序数组中是不是最大的元素
下面贴代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const long long maxn=100005;
long long save[maxn];
long long another[maxn];
int main(){
    int t;
    scanf("%d",&t);
    long long n,k,i,j;
    while(t--){
        memset(another,0,sizeof(another));
        scanf("%lld",&n);
        for(i=1;i<=n;i++){
            scanf("%lld",&save[i]);
        }
        k=1;
        for(i=1;i<=n-1;i++){
            another[k]=abs(save[i+1]-save[i]);
            k++;
        }
        sort(another+1,another+n);
        
        long long result=0;
        if(abs(save[i]-save[i+2])==another[n-1]){
            result += another[n-2];
        }else{
            result += another[n-1];
        }
        if(abs(save[n]-save[n-1])==another[n-1]){
            result += another[n-2];
        }else{
            result += another[n-1];
        }
        for(i=2;i<=n-1;i++){
            if(abs(save[i+1]-save[i-1])>another[n-1]){
                result += abs(save[i+1]-save[i-1]);
            }else{
                k=n-1;
                for(j=1;j<=2;j++){
                    if(another[k]==abs(save[i]-save[i-1])||another[k]==abs(save[i+1]-save[i])){
                        k--;
                    }
                }
                result += another[k];
            }
        }
        printf("%lld\n",result);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值