传送门

NanoApe Loves Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)

Total Submission(s): 1323    Accepted Submission(s): 521

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 nn 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 FF.

Now he wants to know the expected value of FF, 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.
1≤T≤10, 3≤n≤100000, 1≤Ai≤109

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 Output

141 2 3 4

Sample Output

6

思路

题意:

退役狗 NanoApe 滚回去学文化课啦!
在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值。
他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。(为防止精度问题,你需要输出答案乘上n后的值)

 题解:因为最后答案要乘 n ,所以题目变成求,对于每个数,删除它后数列中所有相邻两数的绝对值的最大值的总和。那么我们可以用两个数组求解问题。一个数组保存当前位置及其左边,相邻两数的差的绝对值的最大值,一个数组存当前位置及其右边,相邻两数的差的绝对值的最大值。删除当前位置,那么删除后的数列的相邻两数的绝对值的最大值只需要比较 abs(num[i-1]-num[i+1]),f[i-1],g[i+1]的大小。

 
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
typedef __int64 LL;
int num[maxn],f[maxn],g[maxn];

int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		int N;
		scanf("%d",&N);
		scanf("%d",&num[1]);
		f[0] = 0;
		for (int i = 2;i <= N;i++)
		{
			scanf("%d",&num[i]);
			f[i] = max(f[i-1],abs(num[i] - num[i-1]));
		}
		g[N] = 0;
		for (int i = N - 1;i > 0;i--)
		{
			g[i] = max(g[i+1],abs(num[i+1] - num[i]));
		}
		LL res = 0;
		for (int i = 2;i < N;i++)
		{
			res += max(abs(num[i-1]-num[i+1]),max(f[i-1],g[i+1]));
		}
		res += g[2] + f[N-1];
		printf("%I64d\n",res);
	}
	return 0;
}

  

 

5805 NanoApe Loves Sequence(想法题)的更多相关文章

  1. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  2. HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002

    题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...

  3. HDU 5805 NanoApe Loves Sequence (模拟)

    NanoApe Loves Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5805 Description NanoApe, the ...

  4. HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

    先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 #include <iostream> #inc ...

  5. HDU 5805 NanoApe Loves Sequence

    处理出每个位置左边的最大值和右边的最大值.然后就可以o(1)计算去掉某位置的最大值了. #pragma comment(linker, "/STACK:1024000000,10240000 ...

  6. Best Coder #86 1002 NanoApe Loves Sequence

    NanoApe Loves Sequence Accepts: 531 Submissions: 2481 Time Limit: 2000/1000 MS (Java/Others) Memory ...

  7. NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接:NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 ...

  8. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  9. HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)

    NanoApe Loves Sequence Ⅱ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5806 Description NanoApe, t ...

随机推荐

  1. ContentProvider中央档案馆,以及获取联系人电话的示例

    Android官方文档介绍的数据存储方式共有五种,sqlite,SharedPreferences,网络存储,外储存储,文件存储,但是这些数据都无法进行共享,那么我们就引入了今天的主角:Content ...

  2. 解决Android后台清理APP后,程序自动重启的问题

    最近解决了一个Android APP的bug,发现APP在被后台清理后,会自动重启.现象很奇怪,有的手机(HTC)后台清理后,程序会再次重启,而有的手机(小米)则不会.猜想可能是小米手机内部做了处理, ...

  3. 关于JS交互--调用h5页面,点击页面的按钮,分享到微信朋友圈,好友

    关于js交互,在iOS中自然就想到了调用代理方法 另外就是下面的,直接上代码了: 如果你的后台需要知道你的分享结果,那么,就在回调里面调用上传到服务器结果的请求即可

  4. 高仿ios版美团框架项目源码

    高仿美团框架基本已搭好.代码简单易懂,适合新人.适合新人.新人. <ignore_js_op>     源码你可以到ios教程网那里下载吧,这里我就不上传了,http://ios.662p ...

  5. css选择器的使用详解

    -.css选择器的分类: 二.常用选择器详解: 1.标签选择器: 语法: 标签名 { 属性:属性值; } 代码示例: h1 { color: #ccc; font-size: 28px; } 2.类选 ...

  6. SQL基础教程--实现增删查改功能(W3School)

    1.SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新 ...

  7. MySQL主从复制(Master-Slave)实践

    MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展.多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能. 下图就描述了一个多个数 ...

  8. 织梦dedecms漏洞修复大全(5.7起)

    很多人说dedecms不好,因为用的人多了,找漏洞的人也多了,那么如果我们能修复的话,这些都不是问题. 好,我们来一个一个修复.修复方法都是下载目录下该文件,然后替换或添加部分代码,保存后上传覆盖(记 ...

  9. overflow:hidden与margin:0 auto之间的冲突

    相对于父容器水平居中的代码margin:0 auto与overflow:hidden之间存在冲突.当这两个属性同时应用在一个DIV上时,在chrome浏览器中将无法居中.至于为啥我也不明白.

  10. react自学笔记总结不间断更新

    React React 是由Facfbook维护的一套框架,并且引用到instagram React只是我们熟悉MVC框中的V层,只是视图层面的一个框架,只有俩个半api(createClass,cr ...