NanoApe Loves Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 440 Accepted Submission(s): 205
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.
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.
Sample Input
1 4 1 2 3 4
Sample Output
6
Source
BestCoder Round #86
Recommend
wange2014  |  We have carefully selected several similar problems for you: 5808 5807 5806 5804 5803
Statistic | Submit | Discuss | Note
题目大意:
退役狗 NanoApe 滚回去学文化课啦!
在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值。
他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。
官方题解:
求出前i个数里相邻差值的最大值fi,i到n里相邻差值的最大值gi,那么ans=∑ni=1max(|Ai?1?Ai+1|,fi?1,gi+1)。
时间复杂度O(n)。
个人解题思路:
这个题目应该不是很难的,因为选一个数的概率是 1n 的,然后最后还要乘以 n, 所以 直接就算就行了,首先我们找出最大的前三个数,然后每次判断,当然删掉第一个数 和 最后一个数的时候要特判,删除后的的最大差值是不是与没有删除之前的最大值相等,如果相等的话,加上第二大的差值,否则加上第一大的差值,当在中间的时候,我们需要考虑的是两个数,相当于删除了两个差值,增加了一个差值,我们首先判断增加的那个差值是不是比未删除之前的那个值大,如果比它大的话 就不需要考虑了,直接加上它的后一个数 与 前一个数的差值就行了,否则,判断 删除的那两个差值是不是第一大和第二大,然后 YY 一下就行了。
My Code:
/**
2016 - 08 - 07 上午
Author: ITAK
Motto:
今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1e9+5;
const int MAXN = 1e6+5;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL a[MAXN], sum[MAXN];
inline bool cmp(LL a, LL b)
{
return a > b;
}
int main()
{
int T, n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%I64d",&a[i]);
memset(sum, -1, sizeof(sum));
for(int i=2; i<=n; i++)
sum[i-1] = abs(a[i]-a[i-1]);
sort(sum+1, sum+n, cmp);
LL ans = 0;
if(sum[1] == abs(a[1]-a[2]))
ans += sum[2];
else
ans += sum[1];
if(sum[1] == abs(a[n]-a[n-1]))
ans += sum[2];
else
ans += sum[1];
for(int i=2; i<n; i++)
{
LL t1 = abs(a[i]-a[i-1]);
LL t2 = abs(a[i]-a[i+1]);
LL t3 = abs(a[i+1]-a[i-1]);
if(t3 >= sum[1])
ans += t3;
else
{
if(t1 == sum[1])
{
if(t2 == sum[2])
ans += max(sum[3], t3);
else
ans += max(sum[2], t3);
}
else if(t2 == sum[1])
{
if(t1 == sum[2])
ans += max(sum[3], t3);
else
ans += max(sum[2], t3);
}
else
ans += sum[1];
}
}
printf("%I64d\n",ans);
}
return 0;
}