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 ≤ 105) — the size of the array a.

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

Output

Print the only integer — the maximum value of f.

Example

Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6

Note

In the first sample case, the optimal value of f is reached on intervals [1, 2] and[2, 5].

In the second case maximal value of f is reachable only on the whole array.


  这道题题目大意是说一个二元函数f(l, r),定义见前面那个式子,求最大值。

  首先呢,按照题目意思求差并取绝对值。然后得到的新的序列,求一个下标为奇数的前缀和,下标为偶数的前缀和(不是分开的!)

  例如第一个样例

       1    4    2    3    1

     差   3     2    1    2

奇数位和   0    3    3    4    4

偶数位和     0    0    2    2    4

 和的差   0    3    1    2    0

  下标     0    1     2    3    4

  然后用和的差就可以干很多事情了。你可以试试用下标为奇数的和的差去减之前的下标为偶数的和的差等等,然后得到的数是函数值或取相反数后就是函数值,然后可以干嘛?贪心啊!(至于这个神奇的规律的证明,展开就好了)

Code

 /**
* Codeforces
* Problem#788A
* Accepted
* Time:31ms
* Memory:400k
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef bool boolean;
#define inf 0x3fffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b)) const long long lnf = (1LL << ); int n;
int* a;
long long sodd = , suodd = ;
long long great[][] = {{lnf, -lnf}, {lnf, -lnf}}; //[][0] min [][1] max
long long res = -inf; inline void init() {
scanf("%d", &n);
a = new int[(const int)(n + )];
for(int i = ; i <= n; i++)
scanf("%d", a + i);
for(int i = ; i < n; i++)
a[i] = abs(a[i + ] - a[i]);
} inline void solve() {
if(n == ) {
printf("%d\n", a[]);
return;
}
great[][] = great[][] = ;
for(int i = ; i < n; i++) {
if(i & ) {
sodd += a[i];
long long c = sodd - suodd;
smax(res, c - great[][]);
smax(res, great[][] - c);
smax(great[][], c);
} else {
suodd += a[i];
long long c = sodd - suodd;
smax(res, c - great[][]);
smax(res, great[][] - c);
smin(great[][], c);
}
// cout << sodd << " " << suodd << " " << res << endl;
}
printf("%I64d", res);
} int main() {
init();
solve();
return ;
}

Codeforces 788A Functions again - 贪心的更多相关文章

  1. codeforces 788A Functions again

    …… 原题: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandia ...

  2. CodeForces 788A - Functions again [ DP ]

    反着求一遍最大连续子序列(前项依赖) #include <bits/stdc++.h> using namespace std; #define LL long long ; int n; ...

  3. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  4. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  5. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  6. CodeForces 176A Trading Business 贪心

    Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...

  7. Codeforces Gym 100803C Shopping 贪心

    Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...

  8. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

  9. Codeforces 1154D - Walking Robot - [贪心]

    题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...

随机推荐

  1. First normal formal Second normal form

    https://en.wikipedia.org/wiki/First_normal_form https://en.wikipedia.org/wiki/Second_normal_form A r ...

  2. kubernetes实战(十六):k8s高可用集群平滑升级 v1.11.x 到v1.12.x

    1.基本概念 升级之后所有的containers会重启,因为hash值会变. 不可跨版本升级. 2.升级Master节点 当前版本 [root@k8s-master01 ~]# kubeadm ver ...

  3. angular前端框架

    总所周知,在前端开发中,大家用的比较多的框架就是angular,vue,react等,今天就为大家讲一下angular大家框架的原理及运用 1.本次所举的例子是以依赖require.js的, < ...

  4. 【笔记】DataTable或IList使用GroupBy方法的lamda表达式

    DataTable GroupBy的用法 var result = dt.AsEnumerable(). GroupBy(g => new { StaffID = g.Field<stri ...

  5. git提交时候出错

    Please make sure you have the correct access rights and the repository exists. 解决方案: 主要原因是没有加载keygen ...

  6. Python: ord()函数

    ch() , unichr() , ord() ①chr()函数用一个范围在range(256)内的整数作参数,返回一个对应的字符. >>>chr(65) 'A' ②unichr() ...

  7. linux常用命令:chgrp 命令

    在 lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令变更文件与目录所属群组,这种方式采用群组名称或群组识别 码都可以.chgrp命令就是change group ...

  8. LoadRunner 自动关联、手动关联的帖子

    https://www.guru99.com/correlation-in-loadrunner-ultimate-guide.html 这个网页里介绍了关联的概念,自动关联和手动关联的知识...

  9. python进程join()函数理解

    Join()是主程序等我这个进程执行完毕了,程序才往下走  

  10. Linux服务器配置---安装centos

    安装centos 1.插入光盘,启动,可以选择第一项进行安装 2.根据实际需求,一般会选择skip 3.选择语言“简体中文” 4.选择第一项 5.设置主机名字,使用默认 6.选择时区 7.设置超级用户 ...