HDU 5805 NanoApe Loves Sequence

来源:互联网 发布:15个java小项目 文库 编辑:程序博客网 时间:2024/06/10 07:24

http://acm.hdu.edu.cn/showproblem.php?pid=5805


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.

1T10, 3n100000, 1Ai109
 

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
141 2 3 4
 

Sample Output
6
 



#include<cstdio>#include<iostream>#include<algorithm>#include<cstdlib>using namespace std;typedef long long int ll;int a[100050];int b[100050];bool cmp(int a,int b){return a>b;}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);int i;for(i=0;i<n;i++){scanf("%d",&a[i]);if(i!=0) b[i-1]=abs( a[i]-a[i-1] );}sort(b,b+n-1,cmp);ll ans=0;if( abs( a[0]-a[1] )==b[0] ) ans+=b[1];else ans+=b[0];for(i=1;i<=n-2;i++){int mx;if( abs( a[i]-a[i-1] )==b[0]||abs( a[i]-a[i+1] )==b[0] ){mx=b[1];if( abs( a[i]-a[i-1] )==b[1]||abs( a[i]-a[i+1] )==b[1] ){if( n==3 ) mx=0;else mx=b[2];}}else mx=b[0];mx=max( mx ,abs( a[i-1]-a[i+1] ) );ans+=mx;}if( abs( a[n-1]-a[n-2] )==b[0] ) ans+=b[1];else ans+=b[0];cout<<ans<<"\n";}return 0;}




ps

1.ans要用long long。

2.要比较b数组里第一二三大的数。

3.注意a只有三个元素的情况。

0 0
原创粉丝点击