洛谷垃圾陷阱(dp)

 

 

 

确定dp代表的含义如上图,代码采用一维dp 

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
int dp[maxn];
int D, G;
struct Trash
{
    int t, f, h;
}tra[maxn];


bool cmp(Trash a, Trash b)
{
    return a.t < b.t;
}


int main()
{
    cin >> D >> G;
    for(int i = 1; i <= G; i++)
    {
        cin >> tra[i].t >> tra[i].f >> tra[i].h;
    }
    dp[0] = 10;
    sort(tra + 1, tra + G + 1, cmp);

    for(int i = 1; i <= G; i++)
    {
        for(int j = D; j >= 0; j--)
        {
            if(dp[j] >= tra[i].t)
            {
                dp[j + tra[i].h] = max(dp[j], dp[j + tra[i].h]);
                if(j + tra[i].h >= D)
                {
                    cout << tra[i].t << endl;
                    return 0;
                }
                dp[j] = max(dp[j], dp[j] + tra[i].f);
            }
        }
    }
    cout << dp[0] << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38577732/article/details/89717093