codeforces 864 E. Fire(背包+思维)

题目链接:http://codeforces.com/contest/864/problem/E

 

题解:这题一看就很像背包但是这有3维限制也就是说背包取得先后也会对结果有影响。所以可以考虑sort来降低维度(这是常用的方法)

然后就是简单的有限背包至于这题还要求存下娶了哪些东西可以用vector来存。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#define inf 0X3f3f3f3f
using namespace std;
int dp[2345];
struct TnT {
    int t , d , p , id;
}th[123];
bool cmp(TnT a , TnT b) {
    return a.d < b.d;
}
vector<int>vc[2345];
int main() {
    int n;
    scanf("%d" , &n);
    for(int i = 0 ; i < n ; i++) {
        scanf("%d%d%d" , &th[i].t , &th[i].d , &th[i].p);
        th[i].id = i + 1;
    }
    sort(th , th + n , cmp);
    for(int i = 0 ; i <= 2000 ; i++) dp[i] = -inf , vc[i].clear();
    dp[0] = 0;
    for(int i = 0 ; i < n ; i++) {
        for(int j = th[i].d - 1 ; j >= th[i].t ; j--) {
            if(dp[j - th[i].t] + th[i].p > dp[j]) {
                vc[j].clear();
                int len = vc[j - th[i].t].size();
                for(int l = 0 ; l < len ; l++) {
                    vc[j].push_back(vc[j - th[i].t][l]);
                }
                vc[j].push_back(th[i].id);
                dp[j] = dp[j - th[i].t] + th[i].p;
            }
        }
    }
    int Max = -inf , pos = 0;
    for(int i = 0 ; i <= 2000 ; i++) {
        if(Max < dp[i]) {
            Max = dp[i];
            pos = i;
        }
    }
    printf("%d\n" , Max);
    printf("%d\n" , vc[pos].size());
    for(int i = 0 ; i < vc[pos].size() ; i++) {
        printf("%d " , vc[pos][i]);
    }
    puts("");
    return 0;
}

 

posted @ 2017-09-28 10:36  Gealo  阅读(380)  评论(1编辑  收藏  举报