H - Fire CodeForces - 864E 01背包

https://codeforces.com/problemset/problem/864/E

 

这个题目要把这个按照物品毁灭时间进行排序,如果时间短就要排在前面,这个是因为要保证之后的物品的拯救不会影响到之前的。

 

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 2e3 + 10;

struct node
{
    int t, p, d, id;
}exa[maxn];

bool cmp(node a,node b)
{
    return a.d < b.d;
}

int G[110][2200];
int dp[2200];

int main()
{
    int n;
    int V = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &exa[i].t, &exa[i].d, &exa[i].p);
        exa[i].id = i;
        V = max(V, exa[i].d);
    }
    sort(exa + 1, exa + 1 + n, cmp);
    //for (int i = 1; i <= n; i++) printf("www %d %d %d\n", exa[i].t, exa[i].d, exa[i].p);
    int ex = 0, x = 0, y = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = exa[i].d-1; j >= exa[i].t; j--)
        {
            if (dp[j - exa[i].t] + exa[i].p > dp[j])
            {
                dp[j] = dp[j - exa[i].t] + exa[i].p;
                G[i][j] = 1;
            }
            if(dp[j]>ex)
            {
                ex = dp[j];
                x = i, y = j;
            }
        }
    }
    printf("%d\n", ex);
    stack<int>ans;
    int i = x, j = y;
    while (i > 0)
    {
        if(G[i][j])
        {
            ans.push(exa[i].id);
            j -= exa[i].t;
        }
        i -= 1;
    }
    printf("%d\n", (int)ans.size());
    while(!ans.empty())
    {
        printf("%d ", ans.top());
        ans.pop();
    }
    printf("\n");
    return 0;
}

 

posted @ 2019-05-26 10:54  EchoZQN  阅读(132)  评论(0编辑  收藏  举报