Codeforces Round #407 C

Posted %%%%%

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于Codeforces Round #407 C的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #407 (Div. 2)

Codeforces Round #407 (Div. 2)解题报告

Codeforces Round #407 (Div. 2)(争取明天24点前补掉)

Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A,B,C真的菜·(代

Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators二分查找(示例(代

Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 树状数组维护区间最大值(示(代