题目链接:http://codeforces.com/contest/789/problem/C
题意:
解法:我们会发现我们选的子段要么第一个数是正的,要么第一个数是负,所以我们只需要copy一下原来的数组做两次最大字段和,开始还在思考two pointers,稍微想一些***的题啊,队友昨天上紫了,然而太晚没打,真是可惜。。。现在是真的两个cf div1带我这个cf一直fst,蓝号都回不去的弱鸡了。
//CF 789C
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
template <class T1, class T2> inline void gmax(T1 &x, T2 y) {x = max(x, y);}
int a[maxn], b[maxn], c[maxn], d[maxn];
long long ans;
int main()
{
int n;
scanf("%d", &n);
long long ans = 0;
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 1; i < n; i++){
b[i] = abs(a[i] - a[i+1]);
gmax(ans, 1LL*b[i]);
}
for(int i = 1; i < n; i++){
if(i%2 == 1) c[i] = -b[i];
else c[i] = b[i];
}
for(int i = 1; i < n; i++){
if(i%2 == 1) d[i] = b[i];
else d[i] = -b[i];
}
long long cur = 0;
for(int i = 1; i < n; i++){
cur += 1LL*c[i];
if(cur > ans){
ans = cur;
}
if(cur < 0){
cur = 0;
}
}
cur = 0;
for(int i = 1; i < n; i++){
cur += 1LL*d[i];
if(cur > ans){
ans = cur;
}
if(cur < 0){
cur = 0;
}
}
cout << ans << endl;
return 0;
}