poj 1821

来源:互联网 发布:淘宝网卖妇婴用品 编辑:程序博客网 时间:2024/06/09 23:17

把转移方程优化一下,改变决策顺序就行了。。。


#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <string>#include <map>#include <vector>#include <stack>#include <queue>#include <utility>#include <iostream>#include <algorithm>template<class Num>void read(Num &x){    char c; int flag = 1;    while((c = getchar()) < '0' || c > '9')        if(c == '-') flag *= -1;    x = c - '0';    while((c = getchar()) >= '0' && c <= '9')        x = (x<<3) + (x<<1) + (c-'0');    x *= flag;    return;}template<class Num>void write(Num x){    if(x < 0) putchar('-'), x = -x;    static char s[20];int sl = 0;    while(x) s[sl++] = x%10 + '0',x /= 10;    if(!sl) {putchar('0');return;}    while(sl) putchar(s[--sl]);}#define REP(__i,__st,__ed) for(int __i = (__st); __i <= (__ed); __i++)#define _REP(__i,__st,__ed) for(int __i = (__st); __i >= (__ed); __i--)const int maxn = 1e5 + 50, maxk = 105;const long long LINF = 0x3f3f3f3f3f3f3f3fLL;struct type_worker {    int L, P, S;    void scan()    {        read(L), read(P), read(S);    }}wk[maxk];bool cmp(const type_worker &a, const type_worker &b){    return a.S < b.S;}int N, K;long long dp[maxk][maxn];#define calc(x, y) (dp[x - 1][y] - (y)*wk[x].P)long long solve(){//  f[i][j] = f[i-1][k] + (j - k)*P[i] = (f[i-1][k] - k*P[i]) + j*P[i]// 0 <= j - k < Li     j - Li <= k < Si <= j     for(int i = 1; i <= K; i++)    {        long long maxv = -LINF;        for(int p = std::max(std::min(wk[i].S + wk[i].L - 1, N) - wk[i].L, 0); p < wk[i].S; p++) maxv = std::max(maxv, calc(i, p));        for(int j = std::min(wk[i].S + wk[i].L - 1, N); j >= wk[i].S ; j--)        {            if(j - wk[i].L >= 0) maxv = std::max(maxv, calc(i, j - wk[i].L));            dp[i][j] = std::max(0LL, maxv + j*wk[i].P);        }        for(int j = 1; j <= N; j++)            dp[i][j] = std::max(dp[i][j], std::max(dp[i - 1][j], dp[i][j - 1]));    }    return dp[K][N];}void init(){    read(N), read(K);    REP(i, 1, K) wk[i].scan();    std::sort(wk + 1, wk + K + 1, cmp);}int main(){#ifndef ONLINE_JUDGE    freopen("1821.in","r",stdin);    freopen("1821.out","w",stdout);#endif    init(), write(solve());#ifndef ONLINE_JUDGE    fclose(stdin);    fclose(stdout);#endif    return 0;           }
0 0
原创粉丝点击