首页 技术 正文
技术 2022年11月19日
0 收藏 660 点赞 4,792 浏览 2227 个字

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:

Codeforces 788A Functions again – 贪心

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898