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 withn 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 ofF , if he deleted each number with equal probability.
In math class, NanoApe picked up sequences once again. He wrote down a sequence with
Now he wants to know the expected value of
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 integern , denoting the length of the original sequence.
The second line of the input containsn 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
The second line of the input contains
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 byn .
In order to prevent using float number, you should print the answer multiplied by
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;}
1.ans要用long long。
2.要比较b数组里第一二三大的数。
3.注意a只有三个元素的情况。
0 0
- HDU 5805 NanoApe Loves Sequence
- HDU 5805:NanoApe Loves Sequence
- hdu 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 - NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence [Ad Hoc]
- Hdu 5805 NanoApe Loves Sequence【思维题】
- HDU 5805 NanoApe Loves Sequence(思维)
- 【HDU】5805 - NanoApe Loves Sequence(模拟)
- HDU 5805 NanoApe Loves Sequence(水~)
- HDU 5805 NanoApe Loves Sequence 线段树
- HDU-5805-NanoApe Loves Sequence(水题)
- 如何使用FireBug?
- 【Nginx】Tengine+Keepalived实现主从模式双机热备
- Lucene读书笔记——2. 构建索引
- 54:Spark中的Tungsten-sort Based Shuffle内幕
- cookie和session小解
- HDU 5805 NanoApe Loves Sequence
- Elasticsearch.Net使用(二)【MVC4 图书管理系统】
- 简易网页游戏——八数码问题游戏
- 55:Spark中的Tungsten内存分配和管理内幕
- 微软认知服务开发实践(5) - 视频检测
- 56:Spark中的Tungsten内存和CPU的优化使用
- JDK and Jre
- 六度空间 地下迷宫探索 哈利·波特的考试 旅游规划 畅通工程之最低成本建设问题
- 欢迎使用CSDN-markdown编辑器