UVa 990 - Diving for Gold DP 0-1

Diving for gold

Problem

John is a diver and a treasure hunter. He has just found the location of a pirate ship full of treasures. The sofisticated sonar system on board his ship allows him to identify the location, depth and quantity of gold in each suken treasure. Unfortunatelly, John forgot to bring a GPS device and the chances of ever finding this location again are very slim so he has to grab the gold now. To make the situation worse, John has only one compressed air bottle.

John wants to dive with the compressed air bottle to recover as much gold as possible from the wreck. Write a program John can use to select which treasures he should pick to maximize the quantity of gold recovered.

The problem has the following restrictions:

  • There are n treasures {(d1,v1), (d2,v2), ... (dn,vn)} represented by the pair (depth, quantity of gold). There are at most 30 treasures.
  • The air bottle only allows for t seconds under water. t is at most 1000 seconds.
  • In each dive, John can bring the maximum of one treasure at a time.
  • The descent time is tdi=w*di, where w is an integer constant.
  • The ascent time is tai=2w*di, where w is an integer constant.
  • Due to instrument limitations, all parameters are integer.

Input

The input to this program consists of a sequence of integer values. Input contains several test cases. The first line of each dataset should contain the values t and w. The second line contains the number of treasures. Each of the following lines contains the depth di and quantity of gold viof a different treasure.

A blank line separates each test case.

Output

The first line of the output for each dataset should contain the maximum amount of gold that John can recover from the wreck. The second line should contain the number of recovered treasures. Each of the following lines should contain the depth and amount of gold of each recovered treasure. Treasures should be presented in the same order as the input file.

Print a blank line between outputs for different datasets.

Sample input

210 4
3
10 5
10 1
7 2

In this sample input, the bottle of compressed air has a capacity of 200 seconds, the constant whas the value 4 and there are 3 treasures, the first one at a depth of 10 meters and worth 5 coins of gold, the second one at a depth of 10 meters and worth 1 coin of gold, and the third one at 7 meters and worth 2 coins of gold.

Sample output

7
2
10 5
7 2
 
 
 
#include <cstdio>
#include <cstring>
const int MAX = 5000;

int t,w, n;
int d[MAX], v[MAX];
int dp[MAX][MAX], ans[MAX];

int max(int a, int b)
{
	return a>b?a:b;
}

int main()
{
//	freopen("in.txt","r",stdin);
	int nCase=0;
	while(scanf("%d %d", &t, &w)==2)
	{
		if(nCase) puts("");
		scanf("%d", &n);
		for(int i=1; i <= n; i++)
			scanf("%d %d", &d[i], &v[i]);
		memset(dp,0,sizeof(dp));
		for(int i=1; i <= n; i++)
		{
			for(int j=0; j <= t; j++)
			{
				int temp = d[i]*w*3;
				if(j >= temp)
					dp[i][j]=max(dp[i-1][j],
							dp[i-1][j-temp]+v[i]);
				else
					dp[i][j] = dp[i-1][j];
			}
		}
		int length=0;
		for(int i=n, j=t; i>0; i--)
		{
			int temp = d[i]*w*3;
			if(j-temp>=0 &&
				dp[i][j]==dp[i-1][j-temp]+v[i])
				{
					ans[length++] = i;
					j -= temp;//这里写成 j-=v[i]了 WA了4次
				}
		}
		printf("%d\n", dp[n][t]);
		printf("%d\n", length);
		for(int i=length-1; i>=0; i--)
			printf("%d %d\n", d[ans[i]], v[ans[i]]);
		nCase++;
	}
	return 0;
}


 
WA 了五次,一个是格式,一个是写快了,唉
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值