NanoApe Loves Sequence

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5805

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.
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 Input


1
4
1 2 3 4

Sample Output


6


##题意:

每次从数组中删除 Ai 并求相邻元素绝对值之差的最大值. (再把Ai放回来)
对以上最大值求和.


##题解:

直接模拟,分别记录位置i左边和右边的数构成的最大值.
结果就是:i左边、i右边、与i相邻两数差 这三者的最大值.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

int num[maxn];

int _left[maxn];

int _right[maxn];

int main(void)

{

//IN;

int t; cin >> t;
while(t--)
{
scanf("%d", &n);
for(int i=1; i<=n; i++) {
scanf("%d", &num[i]);
} memset(_left, 0, sizeof(_left));
memset(_right, 0, sizeof(_right)); for(int i=2; i<=n; i++) {
_left[i] = max(_left[i-1], abs(num[i]-num[i-1]));
} for(int i=n-1; i>=1; i--) {
_right[i] = max(_right[i+1], abs(num[i]-num[i+1]));
} LL ans = 0;
for(int i=1; i<=n; i++) {
int cur = max(_left[i-1], _right[i+1]);
if(i!=1 && i!=n) cur = max(cur, abs(num[i+1]-num[i-1]));
ans += (LL)cur;
} printf("%lld\n", ans);
} return 0;

}

HDU 5805 NanoApe Loves Sequence (模拟)的更多相关文章

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

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

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

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

  3. HDU 5805 NanoApe Loves Sequence

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

  4. Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)

    Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...

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

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

  6. 5805 NanoApe Loves Sequence(想法题)

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

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

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

  8. HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

    若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans + ...

  9. HDU 5806 NanoApe Loves Sequence Ⅱ

    将大于等于m的数改为1,其余的改为0.问题转变成了有多少个区间的区间和>=k.可以枚举起点,二分第一个终点 或者尺取法. #pragma comment(linker, "/STACK ...

随机推荐

  1. 内核MKDEV(MAJOR, MINOR)宏

    版本:linux-2.6.24.4宏:    MKDEV(MAJOR, MINOR);  说明: 获取设备在设备表中的位置.        MAJOR   主设备号        MINOR   次设 ...

  2. python判断文件目录是否存在

    import os os.path.isfile('test.txt')  # 如果不存在就返回False os.path.exists(directory)  # 如果目录不存在就返回False o ...

  3. Codeforces Round #207 (Div. 2)C

    读错题意了..线段树延迟标记 白刷这么多线段树 #include <iostream> #include<cstdio> #include<cstring> #in ...

  4. Struts2系列——struts2的result

    在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...

  5. socket编程---一个简单例子

    服务器端代码(单线程): import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe ...

  6. oracle 11g rac 无法自动启动

    如果以上的操作依然不能使数据库资源自动启动,那么参考下面这篇文章修改资源AUTO_START属性. 查看资源状态: crsctl status resource 资源       -p     crs ...

  7. Java [Leetcode 171]Excel Sheet Column Number

    题目描述: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 解题思路: 循环读取数字,从左向右读取,每次该位字 ...

  8. Windows下FFmpeg快速入门

    本系列文章导航 Windows下FFmpeg快速入门 ffmpeg参数解释 mencoder和ffmpeg参数详解(Java处理视频) Java 生成视频缩略图(ffmpeg) 使用ffmpeg进行视 ...

  9. <十一>面向对象分析之UML核心元素之组件

    组件

  10. <三>面向对象分析之UML核心元素之参与者

    一:版型        --->在UML里有一个概念叫版型.有些书里也称类型,构造型.        --->这个概念是对一个UML元素基础定义的扩展.在同一个元素基础定义的基础上赋予特别 ...