CF 789C DP,最大子段和

题目链接:http://codeforces.com/contest/789/problem/C
题意:
这里写图片描述

解法:我们会发现我们选的子段要么第一个数是正的,要么第一个数是负,所以我们只需要copy一下原来的数组做两次最大字段和,开始还在思考two pointers,稍微想一些好sb的题啊,队友昨天上紫了,然而太晚没打,真是可惜。。。现在是真的两个cf div1带我这个cf一直fst,蓝号都回不去的弱鸡了。

//CF 789C

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
template <class T1, class T2> inline void gmax(T1 &x, T2 y) {x = max(x, y);}
int a[maxn], b[maxn], c[maxn], d[maxn];
long long ans;
int main()
{
    int n;
    scanf("%d", &n);
    long long ans = 0;
    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]);
        gmax(ans, 1LL*b[i]);
    }
    for(int i = 1; i < n; i++){
        if(i%2 == 1) c[i] = -b[i];
        else c[i] = b[i];
    }
    for(int i = 1; i < n; i++){
        if(i%2 == 1) d[i] = b[i];
        else d[i] = -b[i];
    }
    long long cur = 0;
    for(int i = 1; i < n; i++){
        cur += 1LL*c[i];
        if(cur > ans){
            ans = cur;
        }
        if(cur < 0){
            cur = 0;
        }
    }
    cur = 0;
    for(int i = 1; i < n; i++){
        cur += 1LL*d[i];
        if(cur > ans){
            ans = cur;
        }
        if(cur < 0){
            cur = 0;
        }
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值