Transmission Door
nanoape Loves SequenceTime
limit:2000/1000 MS (java/others) Memory limit:262144/131072 K (java/others)
Total Submission (s): 1323 Accepted Submission (s): 521Description
Nanoape, the retired Dog, had returned back to prepare for the national higher Education Entrance examination!
In the math class, Nanoape picked up sequences once again. He wrote a sequence with NN numbers on the paper and then randomly deleted a number in the sequence. After then, he calculated the maximum absolute value of the difference of each of the adjacent remained numbers, denoted as F F.
Now he wants to know the expected value of FF, if he deleted each number with equal probability.
Input
The first line of the input contains an integer T, denoting the number of the 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 a float number, you should print the answer multiplied by N.
Sample Output
1
4
1 2 3 4
Sample Output
6
Ideas
Test Instructions:
nN of the series, and he randomly deleted a number according to his mood, so he got a new sequence, and then he calculated the absolute value of the difference of all adjacent two numbers. He certainly knew that the maximum value would change with the number of changes he had deleted, so he wondered what would be expected if the probability of the total number being removed was equal, the maximum value of the absolute difference. (To prevent the accuracy problem, you need to output the answer by the value of N)
solution
: Because the final answer is to multiply N, so the title becomes seeking, for each number, delete it after the absolute value of all adjacent two numbers in the sum of the maximum. Then we can solve the problem with two arrays. An array holds the maximum values of the absolute value of the current position and its left side, the difference between two adjacent numbers, the absolute value of an array in the current position and its right, and the difference between two adjacent numbers. Delete the current position, then the maximum value of the adjacent two digits of the deleted sequence will only need to compare the size of ABS (Num[i-1]-num[i+1]), f[i-1],g[i+1].
#include <bits/stdc++.h>using namespace Std;const int maxn = 100005;typedef __int64 ll;int num[maxn],f[maxn],g[ Maxn];int Main () {int t;scanf ("%d", &t), while (t--) {int n;scanf ("%d", &n), scanf ("%d", &num[1]), f[0] = 0;for ( int i = 2;i <= n;i++) {scanf ("%d", &num[i]); F[i] = max (F[i-1],abs (Num[i]-num[i-1]));} G[n] = 0;for (int i = n-1;i > 0;i--) {g[i] = max (G[i+1],abs (num[i+1]-num[i]));} LL res = 0;for (int i = 2;i < n;i++) {res + = MAX (ABS (num[i-1]-num[i+1]), Max (f[i-1],g[i+1]));} Res + = G[2] + f[n-1];p rintf ("%i64d\n", res);} return 0;}
5805 Nanoape Loves Sequence