codeforces 788-A. Functions again(dp)

82 篇文章 6 订阅

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.

题目大意:
在n个数中找到这么一个区间【l,r】满足公式要求,也就是相邻两个数之差的绝对值乘以-1的次数次方,奇数位为正,偶数位为负,要求最后和最大。

解题思路:
很容易想到dp,如果从奇数位开始,那么奇数位必然为正,偶数位必然为负,反之,如果从偶数位开始,那么偶数位必然为正,奇数位必然为负。只要分奇偶循环各自走一遍,求最大和就好了。

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long d[100005],s[100005],a[100005],p[100005];
int main()
{
    long long n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
          cin>>s[i];
        for(int i=1;i<n;i++)
          a[i]=abs(s[i]-s[i-1]);
        d[1]=a[1];
        d[2]=max(d[1]-a[2],a[2]);
        for(int i=3;i<n;i+=2)         //奇数位开头 
          d[i]=max(a[i],d[i-2]-a[i-1]+a[i]);
        p[2]=a[2];
        p[3]=max(a[3],p[2]-a[3]);
        for(int i=4;i<n;i+=2)         //偶数位开头 
          p[i]=max(a[i],p[i-2]-a[i-1]+a[i]);
        long long ans=d[1];
        for(int i=3;i<n;i+=2)
          ans=max(ans,d[i]);
        for(int i=2;i<n;i+=2)
          ans=max(ans,p[i]);
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值