您的位置:首页 > 大数据 > 人工智能

codeforce 789 C Functions again (dp)

2017-04-16 00:01 363 查看
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.

Examples

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的最大值。

数据:

   (2 ≤ n ≤ 105)(-109 ≤ ai ≤ 109)

把公式转换一下 变成 b[i]=abs(a[i]-a[i+1]) ,  那么每一位可以取正可以取负, 那么我们dp一下就可以了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
const ll inf = 1e15;
ll a
,b
,dp
[2];
int main()
{
int n,i,j;
cin>>n;
for(i=1;i<=n;i++) cin>>a[i];
for(i=1;i<=n;i++)
dp[i][0]=dp[i][1]=-inf;
for(i=1;i<n;i++)
b[i]=abs(a[i]-a[i+1]);
dp[1][1]=b[1];
ll ans = b[1];
for(i=2;i<n;i++) {
dp[i][0]=dp[i-1][1]-b[i];
dp[i][1]=max(dp[i-1][0],0LL)+b[i];
ans=max(ans,max(dp[i][0],dp[i][1]));
}
cout<<ans<<endl;
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: