Codeforces Round #407 C

C. Functions again

题意:给你一个数列,求函数f(l,r) 的最大值 函数f(l,r)的定义如下

思路:求出新的的数列 ci=|ai-ai+1| f(l,r)即为l到r-1的和 并分奇偶分别计算最大连续子列和

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#define ll long long
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int N=1e5+100;
int a[N],b[N],c[N];
int main(){
    int n;
    cin>>n;
    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]);
        c[i]=b[i];
        if(i&1) b[i]*=-1;
        else c[i]*=-1;
    } //for(int i=1; i<n; ++i) cout<<b[i]<<" ";cout<<"\n";for(int i=1; i<n; ++i) cout<<c[i]<<" ";cout<<"\n";
    ll tmp=0,ans=0;
    for(int i=2; i<n; ++i){
        tmp+=b[i];
        ans=max(tmp,ans);
        if(tmp<0){
            tmp=0;
        }
    }
    tmp=0;
    for(int i=1; i<n; ++i){
        tmp+=c[i];
        ans=max(tmp,ans);
        if(tmp<0){
            tmp=0;
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

posted on 2017-04-30 21:29  lazzzy  阅读(163)  评论(0编辑  收藏  举报

导航