【BZOJ1911】【APIO2010】特别行动队(斜率优化DP)

本文介绍了解决BZOJ1911与APIO2010竞赛题目的方法,该题目要求求解特定条件下的最大值。通过使用动态规划并结合斜率优化技巧,实现了高效的求解过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

click me

Solution

这题跟hdu3507差不多,只是把最小值改成了最大值。

Code

/**************************
Au: Hany01
Date: Jan 7th, 2018
Prob: bzoj1911 & apio2010
Email: hany01@foxmail.com
**************************/

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Ha (1000000007)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("bzoj1911.in", "r", stdin);
    freopen("bzoj1911.out", "w", stdout);
#endif
}

const int maxn = 1000005;

struct Item
{
    LL x, y;
    int id;
}q[maxn];

LL Sum[maxn], dp[maxn], a, b, c;
int n, m, head, tail;

inline bool cmpslope1(LL x1, LL y1, LL x2, LL y2, LL Sum) {
    if ((y2 - y1) < Sum * (x2 - x1)) return 1;
    return 0;
}

inline bool cmpslope2(LL x1, LL y1, LL x2, LL y2, LL x3, LL y3) {
    if ((y3 - y2) * (x2 - x1) <= (y2 - y1) * (x3 - x2)) return 1;
    return 0;
}

int main()
{
    File();
    n = read(); a = read(); b = read(); c = read();
    For(i, 1, n) Sum[i] = Sum[i - 1] + read();
    dp[0] = 0; head = tail = 0; q[head] = (Item){0, 0, 0};
    For(i, 1, n) {
        while (head < tail && !cmpslope1(q[head].x, q[head].y, q[head + 1].x, q[head + 1].y, Sum[i] * 2 * a)) ++ head;
        register int j = q[head].id;
        register LL tmp = Sum[i] - Sum[j];
        dp[i] = dp[j] + tmp * tmp * a + tmp * b + c;
        q[++ tail] = (Item){Sum[i], dp[i] + Sum[i] * Sum[i] * a - Sum[i] * b, i};
        while (head + 1 < tail &&
                !cmpslope2(q[tail - 2].x, q[tail - 2].y, q[tail - 1].x, q[tail - 1].y, q[tail].x, q[tail].y))
            q[tail - 1] = q[tail], -- tail;
    }
    printf("%lld\n", dp[n]);
    return 0;
}
//无边落木萧萧下,不尽长江滚滚来。
//    -- 杜甫《登高》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值