Codeforces Round #436 (Div. 2) E. Fire

来源:互联网 发布:郑州seo外包公司 编辑:程序博客网 时间:2024/06/07 00:25

Description

Polycarp is in really serious trouble — his house is on fire! It’s time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.

Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tb seconds after fire started.
Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp’s house.

Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.
Output

In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.
Examples

input
3
3 7 4
2 6 5
3 7 6
output
11
2
2 3
input
2
5 6 1
3 3 5
output
1
1
1

http://codeforces.com/contest/864/problem/E题意:有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值。只能一件一件的选择物品(即在选择这件物品时需要一定的时间,在这段时间之内不能选择其他物品),选择这件物品只能在失效时间之前选择。问选择的最大价值是多少。

挺水 的一道01背包的记录路径的题目。可是我比赛的时候没有做出来。。
都怪自己之前没有好好看。。
现在看这个用的dfs 的方式输出。。
这里由于有2000,另外每件物品还有上届和下届。所以还是不同的。

至少每次都要有 f[i][j]=f[i-1][j];这个东西,这样才可以把价值传递过去。。

#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<ctime>#include<utility>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> pii;int f[110][2010];int g[110][2010];struct p {    int t,d,p,id;};p a[110];int cmp(p a,p b) {    return a.d<b.d;}void solve(int x,int y,int s){    if(x<0)printf("%d\n",s);    else{        if(!g[x][y])solve(x-1,y,s);        else{            solve(x-1,y-a[x].t,s+1);            printf("%d ",a[x].id);        }    }}int main() {    memset(f,0x80,sizeof f);    f[0][0]=0;    int n;    scanf("%d",&n);    int i,j;    for(i=1;i<=n;i++) {        scanf("%d%d%d",&a[i].t,&a[i].d,&a[i].p);        a[i].id=i;    }    sort(a+1,a+n+1,cmp);    for(int i=1;i<=n;++i){        for(int j=0;j<=2000;++j){            f[i][j]=f[i-1][j];            g[i][j]=0;            if(j>=a[i].t&&j<a[i].d){                if(f[i-1][j-a[i].t]+a[i].p>f[i][j]){                    f[i][j]=f[i-1][j-a[i].t]+a[i].p;                    g[i][j]=1;                }            }        }    }    int ans1=-1,ans2=-1;    for(i=0;i<=2000;i++)        if(f[n][i]>ans1) {            ans1=f[n][i];            ans2=i;        }    printf("%d\n",ans1);    solve(n,ans2,0);}
原创粉丝点击