UVa 990 - Diving for Gold
题目大意:一个潜水者去海底寻找金子,已知有n个有金子的地点,分别给出他们的深度和价值。但是由于潜水者只有一瓶氧气,所以他只能在海底呆有限的时间,问他如何才能在这有限的时间里获得尽可能多的金子,并打印出方案。
dp中的0-1背包问题,得到最大值后根据dp数组可得到解决方案。采用“后i个物品”的规划方向可以使打印方案变得简单一点,详见《算法竞赛入门经典》9.3节。
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std; int d[], v[], dp[][];
vector<int> ans; int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int time, w;
bool first = true;
while (scanf("%d%d", &time, &w) != EOF)
{
int k = time / (*w);
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d%d", &d[i], &v[i]);
for (int i = ; i <= k; i++)
dp[n+][i] = ;
for (int i = n; i > ; i--)
for (int j = ; j <= k; j++)
{
dp[i][j] = dp[i+][j];
if (j >= d[i] && dp[i+][j-d[i]]+v[i] > dp[i][j])
dp[i][j] = dp[i+][j-d[i]]+v[i];
}
if (first) first = false;
else printf("\n");
printf("%d\n", dp[][k]);
ans.clear();
for (int i = , j = k; i <= n; i++)
if (j >= d[i] && dp[i][j] == dp[i+][j-d[i]]+v[i])
{
ans.push_back(i);
j -= d[i];
}
cout << ans.size() << endl;
for (int i = ; i < ans.size(); i++)
{
int idx = ans[i];
printf("%d %d\n", d[idx], v[idx]);
}
}
return ;
}
UVa 990 - Diving for Gold的更多相关文章
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- UVA它11292 - Dragon of Loowater
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- UVA 11292 Dragon of Loowater(简单贪心)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
随机推荐
- JavaScript window.setTimeout() 的详细用法
setTimeout (表达式,延时时间)setTimeout(表达式,交互时间) 延时时间/交互时间是以豪秒为单位的(1000ms=1s) setTimeout 在执行时,是在载入后延迟指定时间后, ...
- Linux下find命令用法小结
find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. find命令的格式:find [-path……] -options [-print -exec -ok] path ...
- zf-关于改绍兴县2个简单的BUG却需要ORACLE数据库的感慨
装了一天你的数据库,其实可以直接检出拿到后台代码,然后远程实施让他进项目,我在他的项目上找action,找图片都是一样的,有时候需求文档上也是会截图到action的,蛋疼,这么简单的方法我居然忘记了.
- mysql 查询 字段的类型
select column_name,data_type from information_schema.columnswhere table_name = '表名'
- 理解用requireJs 来实现javascript的模块化加载
这是我看到的一片关于requirejs的初学者的文章,写的不错,下面结合自己的理解记录一下: 原文:http://www.sitepoint.com/understanding-requirejs-f ...
- Diamond Collector
Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...
- SDAU课程练习--problemB(1001)
题目描述 There is a pile of n wooden sticks. The length and weight of each stick are known in advance. T ...
- c#如何序列化与反序列化json文件
见网站: http://www.json.org/ 代码如下: /* * Copyright (c) 2013 Calvin Rien * * Based on the JSON parser by ...
- PhpStorm 10.0注册
ntelliJ IDEA开源社区 提供了如下通用激活方法:注册时选择License server,填http://idea.lanyus.com/,然后点击 OK,再点一次OK,就搞定了.注意http ...
- win7 安装 vagrant + centos + virtualbox
Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用,“代码在我机子上运行没有问题”这种说辞将成为历史. 我的是在win7系 ...