HDU 5805 NanoApe Loves Sequence (BC #86)

原创 2016年08月29日 18:21:45

NanoApe Loves Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 1089    Accepted Submission(s): 449


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

Sample Output
6

题意:给出n个数字,求每个数字删除之后相邻数字差的绝对值的最大值的期望。

分析:因为要求输出期望*n,所以只需要把删除每个数字后,相邻数字差的绝对值的最大值(记为d)加起来即可。

删除一个数字最多关系两个d,所以,预先记录删除前的最大值、次大值、第三大值。判断每个删除的数字是否是d两边的数字。不是就加上最大值,依次类推。

代码如下:

#include <stdio.h>
#include <math.h>
int a[100010];
int main()
{
	int T;
	int n,i,j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%d",a+i);
		long long d=0,max1=-1,max2=-1,max3=-1,index1,index2,index3;
		for(i=0;i<n-1;i++)
		{
			d=fabs(a[i]-a[i+1]);
			if(d>max1)
			{
				max3=max2;
				index3=index2;
				max2=max1;
				index2=index1;
				max1=d;
				index1=i;
			}
			else if(d>max2)
			{
				max3=max2;
				index3=index2;
				max2=d;
				index2=i;
			}
			else if(d>max3)
			{
				max3=d;
				index3=i;
			}
		}
		long long sum=0;
		for(i=1;i<n-1;i++)
		{
			d=fabs(a[i-1]-a[i+1]);
			if(i!=index1 && i!=index1+1 && d<max1)
				sum+=max1;
			else if(i!=index2 && i!=index2+1 && d<max2)
				sum+=max2;
			else if(i!=index3 && i!=index3+1 && d<max3)
				sum+=max3;
			else
				sum+=d;
		}
		if(index1!=0)
			sum+=max1;
		else if(index2!=0)
			sum+=max2;
		
		if(index1+1!=n-1)
			sum+=max1;
		else if(index2+1!=n-1)
			sum+=max2;
		
		printf("%I64d\n",sum);
	}
	
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

HDU 5805/BC 86B NanoApe Loves Sequence

题解:首先计算出每两个相邻数之间的差值,统计出其中最大的三个。然后枚举删每一个点,对于删除当前点,首先删除当前点与左右两个点之间的距离 (只有删除的距离为最大的两个时才会有影响),然后处理没被删掉的最...

HDU 5805 NanoApe Loves Sequence

Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Highe...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

HDU 5805 NanoApe Loves Sequence

题意是说给你一个串,然后任意删去一个位置的数字,然后求整个串相邻两个数的差的绝对值的最大值,最后问你这个最大值的期望值 * 串的长度 等于多少。 先观察,发现每个位置的值来源一定是剩余位置的最大值 ...

hdu 5805 NanoApe Loves Sequence

有一些小技巧,被坑了……

HDU 5805 - NanoApe Loves Sequence

Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher ...

HDU-5805-NanoApe Loves Sequence(水题)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5805 题意:给出一个数组,求去掉1个数字后相邻两数字差的最大值的期望*n, 题解:就是求去掉每个数字后...

HDU 5805 NanoApe Loves Sequence(思维)

思维

【杭电 5805 8.6 BC B NanoApe Loves Sequence】

NanoApe Loves SequenceProblem Description NanoApe, the Retired Dog, has returned back to prepare fo...

hdoj 5805 NanoApe Loves Sequence(模拟)

官方题解: 求出前ii个数里相邻差值的最大值f_if​i​​,ii到nn里相邻差值的最大值g_ig​i​​,那么ans=\sum_{i=1}^n \max(|A_{i-1}-A_{i+1...

Hdu 5805 NanoApe Loves Sequence【思维题】

NanoApe Loves Sequence    Accepts: 531    Submissions: 2481  Time Limit: 2000/1000 MS (Java/O...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)