题目大意:一个潜水者去海底寻找金子,已知有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的更多相关文章

  1. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  2. UVA它11292 - Dragon of Loowater

    Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...

  3. UVA 11292 Dragon of Loowater(简单贪心)

    Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...

  4. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  5. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  6. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  7. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  8. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  9. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

随机推荐

  1. 开源企业管理软件 ONES

    ONES 不是 ONS,基于AngularJS + ThnkPHP开发的企业管理系统平台,名字可以理解为ONES is a Niubility ERP System 或者 ONES Notonly a ...

  2. java开发地三天——数据库介绍

    又是一天萌萌哒地过去了,今天是处理数据库的部分.SQL Server 2008,这东西是上学期搞MFC的时候接触到的,那时候话说安装就是一个大问题,然后在学SQL语句的时候感觉还好,一切都还过得去.现 ...

  3. [转]PHP经验——PHPDoc PHP注释的标准文档

    文档翻译自:http://en.wikipedia.org/wiki/Phpdoc 标记 用途 描述 @abstract   抽象类的变量和方法 @access public, private or ...

  4. 以前的loginUI

    的 <%@ page language="java" pageEncoding="UTF-8"%> <%@ include file=&quo ...

  5. C++Builder 中动态数组的使用(转)

    源:http://i.cnblogs.com/EditPosts.aspx?opt=1 和AnsiString类型一样,动态数组是为了和DELPHI中的动态数组相兼容而定义,在BCB中,动态数组是用模 ...

  6. jQuery常用及基础知识总结(二)

    JQuery Effects 方法说明 show( ) 显示隐藏的匹配元素.show( speed, [callback] ) 以优雅的动画显示所有匹配的元素,并在显示完成后可选地触发一个回调函数.h ...

  7. Divisor Summation_

    Divisor Summation Problem Description Give a natural number n (1 <= n <= 500000), please tell ...

  8. 安卓EditText按钮

    main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  9. Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)

    题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...

  10. codeforce 611A New Year and Days

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...