C - Functions again CodeForces - 789C (dp、思维)

C - Functions again

 CodeForces - 789C 

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long dp,maxx,dp2,maxx2,ans,a,b;
long long masx(long long a,long long b)
{
    if(a>=b) return a;
    else return b;
}
int main()
{
    int n;
    cin>>n>>a;
    int h=1;
    for(int i=0;i<n-1;++i)
    {
        scanf("%lld",&b);
        int f = abs( a - b ) * h;
        int f2 = abs( a - b ) * -h;
        dp = masx( dp+f, f);
        dp2 = masx( dp2+f2, f2);
        maxx = masx( dp, maxx);
        maxx2 = masx( dp2, maxx2);
        h*=-1;
        a=b;
    }
    ans = masx( maxx, maxx2 );
    printf( "%lld", ans );
}

猜你喜欢

转载自www.cnblogs.com/Cnxz/p/12680510.html

相关文章