洛谷 P1156 垃圾陷阱 题解

题目传送门

dp+排序+01背包

就完事了???

貌似就是这样的

代码:

//dp 排序 01背包
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node {
    int t,h,l;
} c[101];
int d,g;
int ti[101],f[101];
bool cmp(node a,node b) {
    return a.t<b.t;
}
int main() {
    cin>>d>>g;
    for(int i=1; i<=g; i++)
        cin>>c[i].t>>c[i].l>>c[i].h;
    sort(c+1,c+1+g,cmp);
    f[0]=10;
    for(int i=1; i<=g; i++)
        for(int j=d; j>=0; j--)
            if(f[j]>=c[i].t) {
                if(j+c[i].h>=d) {
                    cout<<c[i].t;
                    return 0;
                }
                f[j+c[i].h]=max(f[j+c[i].h],f[j]);
                f[j]+=c[i].l;
            }
    cout<<f[0];
    return 0;
}