hdu NanoApe Loves Sequence
来源:互联网 发布:大学java专业入门课程 编辑:程序博客网 时间:2024/05/21 08:08
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
PS:题意:任意去除一个数,找到每次两两差值绝对值的最大值之和
思路:找掐头去尾后差值最大值且记录差值并排序,相加后再找到去除任意一个数后的差值和未去除数差值中最大值比较,选取最大值相加
#include<stdio.h>#include<algorithm>using namespace std;int a[100020],b[100020],p[100020];int abs(int a){ return a>0?a:-a;}int main(){ int t,n; scanf("%d",&t); while(t--) { long long sum=0,maxx=0,num; int k=1; scanf("%d",&n); scanf("%d",&a[0]); scanf("%d",&a[1]); int w=a[1]-a[0]; b[0]=w; p[0]=w; for(int i=2; i<n; i++)//找掐头或去尾后最大值去相加 { scanf("%d",&a[i]); b[k]=abs(a[i]-a[i-1]); p[k]=b[k]; if(i==n-1) { if(maxx<b[k]) { maxx=b[k]; } if(w>maxx) { sum=maxx; sum+=w; maxx=b[k]; } else { if(maxx<b[k]) { sum=maxx; sum+=b[k]; maxx=b[k]; } else { sum=maxx; sum*=2; } } } else { if(maxx<b[k]) { maxx=b[k]; } } k++; } sort(p,p+k);//记录差值 // printf("%d %d %d %d %lld\n",p[0],p[1],p[2],p[3],sum); for(int i=2; i<n; i++) { num=abs(a[i]-a[i-2]); if(abs(a[i-1]-a[i-2])==p[k-1]&&abs(a[i]-a[i-1])!=p[k-2]||abs(a[i-1]-a[i-2])!=p[k-2]&&abs(a[i]-a[i-1])==p[k-1]) { if(num<p[k-2])//若去除的数牵扯到最大值就加第二大值 sum+=p[k-2]; else sum+=num; } else if(abs(a[i-1]-a[i-2])==p[k-1]&&abs(a[i]-a[i-1])==p[k-2]||abs(a[i-1]-a[i-2])==p[k-2]&&abs(a[i]-a[i-1])==p[k-1]) { if(num<p[k-3])//若去除的数牵扯到最大值和第二大值就加第三大值 sum+=p[k-3]; else sum+=num; } else { if(num<p[k-1]) sum+=p[k-1]; else sum+=num; } //printf("%lld\n",sum); } printf("%I64d\n",sum); }}
0 0
- hdu NanoApe Loves Sequence
- hdu NanoApe Loves Sequence
- hdu 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 5805 NanoApe Loves Sequence
- HDU 5806 NanoApe Loves Sequence
- HDU 5805 NanoApe Loves Sequence [Ad Hoc]
- Hdu 5805 NanoApe Loves Sequence【思维题】
- hdu 5086 NanoApe Loves Sequence 【二分查找】
- hdu 5806 NanoApe Loves Sequence Ⅱ
- HDU 5806 NanoApe Loves Sequence Ⅱ
- Android中Parcel的分析以及使用
- 模九的思考
- Oracle知识库学习指南
- View的工作原理(一)——从ViewRoot和DecorView说起
- 异或运算实现两个数的交换
- hdu NanoApe Loves Sequence
- Java 数字排序
- java内存模型与类加载机制
- SQLserver事务处理
- OPENCV3.0 函数学习2——cvtColor
- 逻辑运算符----java
- AFNetworking 2.x 修改适配IPV6
- hdu5734——Acperience(数学推导)
- POJ 2528 Mayor's posters 线段树+离散化技巧