欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BZOJ1911: [Apio2010]特别行动队(dp 斜率优化)

程序员文章站 2022-10-19 08:22:32
题意 "题目链接" Sol 裸的斜率优化,注意推导过程中的符号问题。 cpp include define Pair pair define MP(x, y) make_pair(x, y) define fi first define se second define int long long ......

题意

题目链接

sol

裸的斜率优化,注意推导过程中的符号问题。

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
#define ll long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 6e18 + 10;
const double eps = 1e-9;
template <typename y> inline void chmin(y &a, y b){a = (a < b ? a : b);}
template <typename y> inline void chmax(y &a, y b){a = (a > b ? a : b);}
template <typename y> inline void debug(y a){cout << a << '\n';}
template <typename y> inline ll sqr(y x){return 1ll * x * x;}
int add(int x, int y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
void add2(int &x, int y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
int mul(int x, int y) {return 1ll * x * y % mod;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, s[maxn], a, b, c, f[maxn], q[maxn];
int f(int x) {
    return a * sqr(x) + b * x + c;
}
double y(int x) {
    return a * sqr(s[x]) - b * s[x] + f[x];
}
double x(int x) {
    return s[x];
}
double slope(int a, int b) {
//  debug((y(b) - y(a)) / (x(b) - x(a)));
    return double(y(b) - y(a)) / (x(b) - x(a));
}
signed main() {
    n = read(); a = read(); b = read(); c = read();
    for(int i = 1; i <= n; i++) s[i] = s[i - 1] + read();
    q[1] = 0;
    for(int i = 1, h = 1, t = 1; i <= n; i++) {
        while(h < t && slope(q[h], q[h + 1]) > 2 * a * s[i]) h++;
        f[i] = f[q[h]] + f(s[i] - s[q[h]]);
        //printf("%d\n", q[h]);
        while(h < t && slope(q[t - 1], q[t]) < slope(q[t], i)) t--;
        q[++t] = i;
    }
    cout << f[n];
    return 0;
}
/*
7
-1 160 -2000
14 82 61 85 41 10 34
*/