Codeforces 864E Fire(题意 详解)

E. Fire
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 201 ≤ di ≤ 2 0001 ≤ 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
Copy
3
3 7 4
2 6 5
3 7 6
output
Copy
11
2
2 3 
input
Copy
2
5 6 1
3 3 5
output
Copy
1
1
1 
Note

In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2seconds. Thus, the total value of the saved items will be 6 + 5 = 11.

In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in3 seconds, but this item will already be completely burned by this time.

题意:

给出N个物品,每个物品拿取需要ti时间,di之间之后这个物品就不能拿了,物品的价值为pi,问怎样拿能够拿最多价值的物品,问这些物品拿取的顺序。

dp[i][j]表示尝试取到第i个物品以时刻j为结束时间的最大价值

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n;
int dp[105][2005];
int pos[105][2005];//pos[i][j]表示在尝试取到第i个物品时以j时刻为结束时间的状态时 是否选取了数组编号为a[i].k的物品 
int b[105];
int ans;//最大价值,物品个数 
struct node
{
	int c,e,v,k;//抢救需要花费的时间,最迟抢救时间,价值 ,第几个物品 
}a[105];
int cmp(node x,node y)
{
    return x.e<y.e;//越紧急的排前面 
}
void print(int i,int j,int num)//逆序  
{
	if(i==0)//n个物品都遍历完了 
	{
		printf("%d\n",num);//就输出被选取的个数 
		return;//开始回溯输出依次选取的物品 
	}
	if(pos[i][j]==0)//该状态不存在(这个物品没有抢救) 
	{
		print(i-1,j,num);//就找上一个物品,时间不变 (没有花费时间),抢救物品的个数也不变 
	}
	else
	{
		print(i-1,j-a[i].c,num+1);//该状态存在,就找上一个物品,减去抢救当前物品花费的时间,抢救物品的个数加1 
		printf("%d ",a[i].k);//递归回溯,输出抢救物品的序号 
	}
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
		scanf("%d %d %d",&a[i].c,&a[i].e,&a[i].v),a[i].k=i;
		sort(a+1,a+n+1,cmp);
		memset(dp,0,sizeof(dp));
		memset(pos,0,sizeof(pos));
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<=2000;j++)
			{
				dp[i][j]=dp[i-1][j];//假设不抢救该物品 
				if(j>=a[i].c&&j<a[i].e)//有时间并且物品还没毁掉
				{
					if(dp[i][j]<dp[i-1][j-a[i].c]+a[i].v)//如果 "不抢救该物品"没有"抢救该物品"获得的最大价值大 
					{
						dp[i][j]=dp[i-1][j-a[i].c]+a[i].v;//就更新当前状态,抢救该物品,否则就默认为不抢救(之前赋过值了) 
						pos[i][j]=1;//标记说明该状态时抢救了该物品 
					}
				} 
			}
		}
		ans=-1;
		int time;//选取找到最大值时的时刻 
		for(int i=0;i<=2000;i++)
		if(dp[n][i]>ans) ans=dp[n][i],time=i;//找出最大价值,这里时间没有明确限制,就比到题目给定的时间范围为止 
		printf("%d\n",ans);
		print(n,time,0);//n个物品,time时长,初始抢救了0个物品,从这里开始调用函数根据最后一个时刻找出的物品逆推 
		printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值