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. HDU1698 Just a Hook —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...

  2. YTU 2852: 二分查找

    2852: 二分查找 时间限制: 1 Sec  内存限制: 128 MB 提交: 215  解决: 79 题目描述 输入不多于20个升序排列的整数,以及一个待查找的数key,输出key在序列中的位置( ...

  3. mysql数据库ip与字符串

    Mysql自带的IP转换语句 inet_aton:将ip地址转换成数字型 inet_ntoa:将数字型转换成ip地址 //使用inet_aton函数,将字符串IP转换为整型: mysql> se ...

  4. 【转】Java 并发编程:volatile的使用及其原理

    一.volatile的作用 在<Java并发编程:核心理论>一文中,我们已经提到过可见性.有序性及原子性问题,通常情况下我们可以通过Synchronized关键字来解决这些个问题,不过如果 ...

  5. BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包

    BZOJ_1670_[Usaco2006 Oct]Building the Moat护城河的挖掘_求凸包 Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场 ...

  6. WIN8系统的远程桌面漏洞 利用QQ拼音纯净版实现提权

    前言 发现这个漏洞的时候, 笔者正在机房上课.正想用3389远程桌面去控制宿舍电脑的时候,因为重做系统忘记自己的IP地址,因此就随手扫描了一下IP段开3389端口的电脑. 没想到就随手扫描到一台WIN ...

  7. VMware虚拟机安装WinXP出现错误output error file to the following location A:\GHOSTERR.TXT

    我们安装Ghost版WinXP系统的时候,可能会出现一个如下图这样的错误:output error file to the following location A:\GHOSTERR.TXT. 出现 ...

  8. appium学习【一】:pycharm运行不生成HtmlTestRunner测试报告

    参考文章和解决办法:http://blog.csdn.net/xie_0723/article/details/50825310                  http://jingyan.bai ...

  9. JS处理Cookie

    <script>function GetCookieVal(offset)//获得Cookie解码后的值{var endstr = document.cookie.indexOf (&qu ...

  10. bzoj 4137 [FJOI2015]火星商店问题【CDQ分治+可持久化trie】

    其实我不太清楚这个应该叫CDQ分治还是整体二分 参考:http://blog.csdn.net/lvzelong2014/article/details/78688727 一眼做法是线段树套可持久化t ...