PAT T1002 Business

背包问题,把任务按截止日期排序,再按背包问题处理~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+14;
struct node {
    int c;
    int w;
    int ddl;
}Node[maxn];
int dp[maxn];
bool cmp (node a,node b) {
    return a.ddl<b.ddl;
}
int main () {
    int N;
    int maxddl=0;
    scanf ("%d",&N);
    for (int i=1;i<=N;i++) {
        scanf ("%d %d %d",&Node[i].w,&Node[i].c,&Node[i].ddl);
        maxddl=max(maxddl,Node[i].ddl);
    }
    sort (Node+1,Node+N+1,cmp);
    for (int i=1;i<=N;i++) {
        for (int j=maxddl;j>=Node[i].c;j--) 
        if (j<=Node[i].ddl) dp[j]=max(dp[j],dp[j-Node[i].c]+Node[i].w); 
    }
    int Max=0;
    for (int i=0;i<=maxddl;i++) Max=max(Max,dp[i]);
    printf ("%d",Max);
    return 0;
}

 

posted @ 2020-02-13 11:12  zlc0405  阅读(142)  评论(0编辑  收藏  举报