当前位置:网站首页>Hdu-5805-nanoape loves sequence (thinking questions)
Hdu-5805-nanoape loves sequence (thinking questions)
2022-07-28 06:51:00 【__ Simon】
NanoApe Loves Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 1087 Accepted Submission(s): 448
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.
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
In order to prevent using float number, you should print the answer multiplied by n.
1 4 1 2 3 4
6
/**
* @Date: 2016-07-21 10-41-21
* @Project: ACM
* @Last modified time: 2016-08-08 01-53-22
*/
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 100005
int n, m;
long long sum;
long long L[maxn], R[maxn];
long long a[100004];
int main () {
int t;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
for (int i = 1; i <= n; i++) scanf ("%lld", &a[i]);
sum = 0;
//printf("==%d==%d\n",L[1],R[n]);
L[1] = -1, R[n] = -1;// The left and right ends are assigned small enough initial values
/**
* Preprocessing , Based on the number to be deleted , Pretreat both sides
*/
for (int i = 2; i <= n; i++) { // Preprocess to the left , Make the absolute value of the maximum difference on the left side L[i]
L[i] = max (L[i-1], abs (a[i]-a[i-1]));
}
for (int i = n-1; i >= 1; i--) { // Pretreatment to the right , Make the absolute value of the maximum difference on the right side R[i]
R[i] = max (R[i+1], abs (a[i]-a[i+1]));
}
/**
* Special treatment is required when deleting numbers at both ends , Get the maximum
* When deleting the middle number , Consider the absolute value of the difference between the number before deletion and the left and right adjacent numbers , And a new difference is generated . Need to discuss and deal with
*/
for (int i = 1; i <= n; i++) {
if (i == 1) sum += R[2]; // When the first number is deleted , Think to the right , here R[2] Is the absolute value of the maximum difference
else if (i == n) sum += L[n-1];// When the last number is deleted , Think to the left , here L[n-1] Is the absolute value of the maximum difference
// When deleting the middle number , First, compare the absolute value of the difference between the number before deletion and the numbers on the left and right sides , Take the larger value , Then compare with the new absolute value of the difference , Taking the maximum
else sum += max (max (L[i-1], R[i+1]), abs (a[i+1]-a[i-1]));
}
printf ("%lld\n", sum);// Sum up , At sight *n Result
}
return 0;
}边栏推荐
猜你喜欢

Prometheus monitoring Nacos

2022 Tanabata gift recommendation! Nice, cheap and practical gift recommendation

技术分享 | 接口测试常用代理工具

Graphic pipeline foundation (part outside)
![[dynamic planning -- the best period for buying and selling stocks Series 2]](/img/6c/887a026d3c1bcbd278bb7f3e0afd05.png)
[dynamic planning -- the best period for buying and selling stocks Series 2]

JS reverse question 100 - question 1

Leetcode brush question diary sword finger offer II 048. serialization and deserialization binary tree

技术分享 | 使用postman发送请求

rancher部署实战

Leetcode skimming diary sword finger offer II 050. sum of downward path nodes
随机推荐
软件测试的生命周期(流程)
Scratch command
[realize the simple version of minesweeping games]
JS reverse question 100 - question 1
[C language] string library function introduction and simulation
About the collation of shader keyword
项目编译NoSuch***Error问题
Ubuntu18.04+Centos7配置redis主从【学习笔记】
Lancher deployment practice
cocos2d-x 学习笔记——瓦片地图TiledMap
yapi漏洞挂马程序chongfu.sh处理
ISO 3.0-server three power separation configuration
What's a good gift for Tanabata? Niche and advanced product gift recommendation
Which is the best and most cost-effective air conduction headset recommended
什么是线性表?
Graphic pipeline foundation (I)
数组转链表
技术分享 | 如何模拟真实使用场景?mock 技术来帮你
Everything you don't know about time complexity is here
单元测试框架Jest搭配TypeScript的安装与配置