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.
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.
1≤T≤10, 3≤n≤100000, 1≤Ai≤109
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.
1≤T≤10, 3≤n≤100000, 1≤Ai≤109
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.
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;
}