C. Functions again
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

Input

The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a.

The second line contains n integers a1, a2, ..., an (-10^9 ≤ ai ≤ 10^9) — the array elements.

Output

Print the only integer — the maximum value of f.

Examples
input
5
1 4 2 3 1
output
3
input
4
1 5 4 7
output
6

题意:求公式的最大值。
思路:
  b[i]:abs(num[i]-num[i+1]),c[N]:-abs(num[i]-num[i+1])  (i%2==1)
  b[i]:-abs(num[i]+num[i+1]),c[N]:abs(num[i]-num[i+1])  (i%2==0)
  然后分别求b,c的区间连续最大值,二者中较大着就是答案。
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define LL long long
#define INF 10000000005
int num[N],b[N],c[N]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&num[i]);
for(int i=;i<n-;i++)
{
if(i%)
{
b[i]=abs(num[i]-num[i+]);
c[i]=-abs(num[i]-num[i+]);
}
else
{
b[i]=-abs(num[i]-num[i+]);
c[i]=abs(num[i]-num[i+]);
}
}
LL res1=-INF,res2=-INF,tmp=,l=;
for(int i=;i<n-;i++)
{
tmp+=b[i];
if(tmp>res1)
res1=tmp;
if(tmp<)
tmp=;
}
tmp=;
for(int i=;i<n-;i++)
{
tmp+=c[i];
if(tmp>res2)
res2=tmp;
if(tmp<)
tmp=;
}
printf("%I64d\n",max(res1,res2));
return ;
}

Codeforces_789C_(dp)的更多相关文章

  1. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  2. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  3. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  4. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  9. android px转换为dip/dp

    /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...

随机推荐

  1. IntentFilter打印方法

    转载请注明出处:http://blog.csdn.net/droyon 在我们进行Android应用程序开发时.我们有时须要对某个对象进行打印输出.以方便我们进行调试. 非常多对象实现了toStrin ...

  2. camera摄像原理之四:曝光和GAMMA

    GAMMA:输出/输入(光信号值) 从最明亮到最黑暗,假设人眼能够看到一定的范围,那么胶片(或CCD 等电子感光器件)所能表现的远比人眼看到的范围小的多,而这个有限的范围就是感光宽容度. 人眼的感光宽 ...

  3. JavaScript 图片广告自动与手动的切换

    ​1.代码 <html> <head>   <script type="text/javascript" src="jquery-1.8.j ...

  4. POJ - 3352 Road Construction(边双连通分支)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2.POJ - 3177 Redundant Paths(边双连通分支)(模板)  与这道题一模一样.代码就改了下范围,其他都没动 ...

  5. c#自动登录

    c#自动登录 http://blog.csdn.net/eastmount/article/details/9703757

  6. UI:沙盒

    IOS平台下,沙盒的本质就是一个文件夹 每一款IOS应用安装在手机上都会自动的生成一个文件夹.之所以叫沙盒,就是因为这个文件夹是每次运行随机产生的文件夹.沙盒文件夹是独立的,每个应用之间不能互相访问. ...

  7. ChartCtrl源码剖析之——CChartAxis类

    CChartAxis类用来绘制波形控件的坐标轴,这个源码相对较复杂,当初阅读的时候耗费了不少精力来理解源码中的一些实现细节. CChartAxis类的头文件. #if !defined(AFX_CHA ...

  8. 03_隐式意图打开activity

    想让第一个activity把第二个activity打开的话,在清单文件里面声明一下并且 右键Debug As Android Application居然没有报错 mimeType  讲HTML的时候就 ...

  9. bzoj 4825: [Hnoi2017]单旋【dfs序+线段树+hash】

    这个代码已经不是写丑那么简单了--脑子浆糊感觉np++分分钟想暴起打死我--就这还一遍A过了-- 先都读进来hash一下,因为是平衡树所以dfs序直接按照点值来就好 对于每个操作: 1:set维护已插 ...

  10. P4357 [CQOI2016]K远点对(KDTree)

    传送门 又一次产生了KDTree本质就是爆搜的感觉-- 大概就类似于p4169,只不过是从最近点对变成了第\(k\)远点对 我们开一个小根堆,里面放\(k\)个元素,起初全为\(0\),然后每一次都把 ...