• BZOJ 1911: [Apio2010]特别行动队( dp + 斜率优化 )


    sum为战斗力的前缀和

    dp(x) = max( dp(p)+A*(sumx-sump)2+B*(sumx-sump)+C )(0≤p<x)

    然后斜率优化...懒得写下去了...

    --------------------------------------------------------------------------------

    #include<bits/stdc++.h>
     
    using namespace std;
     
    typedef long long ll;
     
    const int maxn = 1000009;
     
    int N, A, B, C, Q[maxn], qh, qt;
    ll w[maxn], dp[maxn];
     
    inline ll f(int x) {
    return dp[x] + ll(A) * w[x] * w[x];
    }
     
    inline ll g(int x, int p) {
    return dp[p] + ll(A) * (w[x] - w[p]) * (w[x] - w[p]) + ll(B) * (w[x] - w[p]) + C;
    }
     
    int main() {
    scanf("%d%d%d%d", &N, &A, &B, &C);
    w[0] = 0;
    for(int i = 1; i <= N; i++) {
    scanf("%d", w + i);
    w[i] += w[i - 1];
    }
    qh = qt = 0;
    Q[qt++] = 0;
    dp[0] = 0;
    for(int i = 1; i <= N; i++) {
    ll t = 2LL * A * w[i] + B;
    while(qt - qh >= 2 && f(Q[qh + 1]) - f(Q[qh]) > t * (w[Q[qh + 1]] - w[Q[qh]])) qh++;
    dp[i] = g(i, Q[qh]);
    while(qt - qh >= 2 && (ll) (f(i) - f(Q[qt - 1])) * (w[Q[qt - 1]] - w[Q[qt - 2]]) > (ll) (f(Q[qt - 1]) - f(Q[qt - 2])) * (w[i] - w[Q[qt - 1]])) qt--;
    Q[qt++] = i;
    }
    printf("%lld ", dp[N]);
    return 0;
    }

    --------------------------------------------------------------------------------

    1911: [Apio2010]特别行动队

    Time Limit: 4 Sec  Memory Limit: 64 MB
    Submit: 2998  Solved: 1354
    [Submit][Status][Discuss]

    Description

    Input

    Output

    Sample Input

    4
    -1 10 -20
    2 2 3 4

    Sample Output

    9

    HINT

    Source

  • 相关阅读:
    poj 1573 Robot Motion
    poj 1035 Spell checker
    poj 3080 Blue Jeans
    poj 3468 A Simple Problem with Integers(线段树区间更新)
    poj 3687 Labeling Balls(拓补排序)
    poj 3295 Tautology
    poj 1062 昂贵的聘礼
    hdu 1170 Balloon Comes!
    hdu 1215 七夕节
    OCJP-试题集合 | 对象的比较
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4811459.html
Copyright © 2020-2023  润新知