Nanoape Loves Sequence
Time limit:2000/1000 MS (java/others) Memory limit:262144/131072 K (java/others)
Total submission (s): Accepted submission (s): 205
Problem Descriptionnanoape, the retired Dog, had returned back to prepare for the national higher Education Entrance Exami nation!
In the math class, Nanoape picked up sequences once again. He wrote down a sequence with< Span id= "mathjax-span-2" class= "Mrow" >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 of the adjacent remained numbers, denoted as Span class= "Mathjax_preview" > F .
Now he wants to know the expected value of f , if he deleted each number with equal probability.
Inputthe first line of the input contains an integerT, denoting the number of test cases.
In each test case, the first line of the input contains an integerN, denoting the length of the original sequence.
The second line of the input containsNIntegersa1,a2,.. . ,An , denoting the elements of the sequence.
1≤t≤ 10, 3≤n≤100000, 1≤ a I≤ 109
Outputfor 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 Input141 2 3 4
Sample OUTPUT6
Source Bestcoder Round #86
Recommendwange2014 | We have carefully selected several similar problems for you:5808 5807 5806 5805 5804
#include <iostream>#include<cstdio>#include<cmath>using namespacestd;intMain () {intT; intN; intCha=0; intCha2=0; inta[100005]={0}; intmaxx1; intFirst ; intmaxx2; intsecond; intmaxx3; intthird; intsum=0; scanf ("%d",&t); for(intz=0; z<t;z++) {sum=0; Maxx1=0; MAXX2=0; MAXX3=0; scanf ("%d",&N); for(intI=0; i<n;i++) {scanf ("%d",&A[i]); if(i!=0) {Cha=abs (a[i]-a[i-1]); if(maxx1<cha) {maxx1=cha; First=i; } } } for(intI=1; i<n;i++) {Cha=abs (a[i]-a[i-1]); if(maxx2<cha) { if(maxx2<=maxx1) { if(i==First ) { Continue; }Else{maxx2=cha; Second=i; } } } } for(intI=1; i<n;i++) {Cha=abs (a[i]-a[i-1]); if(maxx2<cha) { if(maxx3<=maxx1&&maxx3<=maxx2) { if(i==first| | i==second) { Continue; }Else{maxx3=cha; Third=i; } } } } for(intI=1; i<n-1; i++) {CHA2=abs (a[i+1]-a[i-1]); if(maxx1<=CHA2) {Sum+=CHA2; } if(maxx1>CHA2) { if(i==first&&i+1==second| | i==second&&i+1==First ) { if(maxx3!=0){ if(maxx3>=CHA2) {Sum+=maxx3; }Else{sum+=CHA2; } }Else{sum+=CHA2; } } if(i==first| | i==first-1){ if(cha2<=maxx2) {Sum+=maxx2; }Else{sum+=CHA2; } }Else{sum+=maxx1; } } } if(first==1) {sum+=maxx2; Sum+=maxx1; } if(first==n-1) {sum+=maxx2; Sum+=maxx1; } if(first!=1&&first!=n-1) {sum+=(2*maxx1); } printf ("%d\n", sum); } return 0;}
Nanoape Loves sequence-to be solved