当前位置:网站首页>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;
}边栏推荐
- Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones
- 技术分享 | 接口自动化测试中,如何做断言验证?
- JS reverse question 100 - question 1
- Scratch command
- Graphic pipeline foundation (II)
- 技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?
- 浅谈Cookie和Session
- [explain in detail how to realize Sanzi chess step by step]
- 遍历 二叉树
- What kind of air conduction Bluetooth headset with good configuration is recommended
猜你喜欢
![[dynamic planning -- the best period series for buying and selling stocks]](/img/90/9060eabc9b9e7f660504806fde282d.png)
[dynamic planning -- the best period series for buying and selling stocks]
![[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]

Battle plague Cup -- strange shape

MySQL master master

技术分享 | 实战详解接口测试请求方式Get、post

mongoDB快速入门
![[pta---- output full arrangement]](/img/66/d1699cd55fa5ff4a55e3e150d02c1b.png)
[pta---- output full arrangement]

设计测试用例的方法

QGraphicsView提升为QChartView

MySQL master-slave
随机推荐
QGraphicsView提升为QChartView
技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?
遍历 二叉树
Prometheus monitoring Nacos
浅谈Cookie和Session
手把手教你三步完成测试监控系统搭建
How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with
进程和线程的区别
Explain in detail
OJ 1018 counting game
技术分享 | 如何模拟真实使用场景?mock 技术来帮你
单项链表的创建、遍历以及按要求查找结点
技术分享 | 使用postman发送请求
mongoDB复制集及分片集群
JS逆向100题——第1题
NiO example
[the beginning of self redemption]
测试面试题集锦(三)| 计算机网络和数据库篇(附答案)
Fermat's theorem
测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)