BZOJ 1855 股票交易 (算竞进阶习题)

单调队列优化dp

dp真的是难。。不看题解完全不知道状态转移方程QAQ

推出方程后发现是关于j,k独立的多项式,所以可以单调队列优化。。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 3000;
int t, p, w, ap[N], bp[N], as[N], bs[N], dp[N][N], q[N];

int calc1(int i, int k){
    return dp[i - w - 1][k] + k * ap[i];
}

int calc2(int i, int k){
    return dp[i - w - 1][k] + k * bp[i];
}

int main(){

    t = read(), p = read(), w = read();
    for(int i = 1; i <= t; i ++){
        ap[i] = read(), bp[i] = read();
        as[i] = read(), bs[i] = read();
    }
    full(dp, 0xcf);
    for(int i = 1; i <= t; i ++){
        for(int j = 0; j <= as[i]; j ++) dp[i][j] = -1 * ap[i] * j;
        for(int j = 0; j <= p; j ++) dp[i][j] = max(dp[i][j], dp[i - 1][j]);
        if(i <= w) continue;
        int l = 1, r = 0;
        for(int j = 0; j <= p; j ++){
            while(l <= r && j - q[l] > as[i]) l ++;
            while(l <= r && calc1(i, q[r]) <= calc1(i, j)) r --;
            q[++r] = j;
            if(l <= r) dp[i][j] = max(dp[i][j], calc1(i, q[l]) - j * ap[i]);
        }
        l = 1, r = 0;
        for(int j = p; j >= 0; j --){
            while(l <= r && q[l] > j + bs[i]) l ++;
            while(l <= r && calc2(i, q[r]) <= calc2(i, j)) r --;
            q[++r] = j;
            if(l <= r) dp[i][j] = max(dp[i][j], calc2(i, q[l]) - j * bp[i]);
        }
    }
    int ans = -INF;
    for(int i = 0; i <= p; i ++){
        ans = max(ans, dp[t][i]);
    }
    printf("%d\n", ans);
    return 0;
}

posted @ 2019-04-24 15:52  清楚少女ひなこ  阅读(144)  评论(0编辑  收藏  举报