HDU 5805 NanoApe Loves Sequence (模拟)
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 (模拟)的更多相关文章
- HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002
题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...
- HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)
先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 #include <iostream> #inc ...
- HDU 5805 NanoApe Loves Sequence
处理出每个位置左边的最大值和右边的最大值.然后就可以o(1)计算去掉某位置的最大值了. #pragma comment(linker, "/STACK:1024000000,10240000 ...
- Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)
Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...
- HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)
NanoApe Loves Sequence Ⅱ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5806 Description NanoApe, t ...
- 5805 NanoApe Loves Sequence(想法题)
传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K ( ...
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- 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 + ...
- HDU 5806 NanoApe Loves Sequence Ⅱ
将大于等于m的数改为1,其余的改为0.问题转变成了有多少个区间的区间和>=k.可以枚举起点,二分第一个终点 或者尺取法. #pragma comment(linker, "/STACK ...
随机推荐
- PHP Redis 集群封装类
<?php /** * Redis 操作,支持 Master/Slave 的负载集群 * * @author V哥 */ class RedisCluster{ // 是否 ...
- WC约束示使用
1.接口中添加时要注意Xml序列化标签不要随意添加啊.[webInvoke].[OpertorContract]这2个约束就行了.不然,直接坑死啊.
- HDU 4686 Arc of Dream(矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 思路: #include <iostream>#include <cs ...
- Windows SDK 实现不规则窗口介绍
不规则窗口在程序界面设计中能提供非常好的用户体验,以下是我程序运行时的效果图: 以下是代码,注意需要修改一些简单的位置,如资源ID,项目的头文件等,这些是根据你创建的win32程序的项目名改变的,我的 ...
- ASP.NET MVC ActionResult的实现
1.11种ActionResult 在System.Web.Mvc命名空间下: ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 ...
- libogg.so fro android编译方法
在网站下载源代码http://www.xiph.org/downloads/ 解压然后打开cygwin配置 CXX=arm-linux-androideabi-g++.exe LD=arm-linux ...
- OK335xS psplash Screen 移植
/*********************************************************************** * OK335xS psplash Screen 移植 ...
- MVC&WebForm对照学习:传值方式
刚从webform开发转到mvc,如果说像路由这样稍微复杂一点的知识点还可以暂时先放一放(前提是默认的路由规则基本满足大部分需求),那有个问题在快速开发中,我想是必须要当即解决的,那就是webform ...
- JAVA模块化
今天转载JAVA模块化系列的三篇文章. 在过去几年,Java模块化一直是一个活跃的话题.从JSR 277(现已废止)到JSR 291,模块化看起来是Java进化过程中的必经一环.即便是基于JVM的未来 ...
- android view的setVisibility方法值的意思
android view的setVisibility方法值的意思 有三个值 visibility One of VISIBLE, INVISIBLE, or GONE. 常量值为0,意思是可见的 常 ...