题意:

   要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖。给出身上的钱和所有东西的3个属性,求最大总价值。

思路:

  1)WA思路:与01背包差不多,dp过程中记录每个容量所能获得的最大价值以及剩余的容量。实现是,开个二维dp数组,从左往右扫,考虑背包容量为j的可获得价值量,根据该剩余容量得知要更新后面哪个背包容量[j+?] ,如果剩余容量大于q[i]那么可以直接装进去,更新价值以及剩余容量,否则,考虑更新的是更大的背包容量。这个思路有缺陷,一直找不到。

  2)AC思路:先看代码,下面再证明其正确性。

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int p,q,v;
} a[]; int cmp(node x,node y)//按q-p排序,保证差额最小为最优
{
return x.q-x.p<y.q-y.p;
} int main()
{
int n,m,i,j;
int dp[];
while(~scanf("%d%d",&n,&m))
{
for(i = ; i<n; i++)
scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);
memset(dp,,sizeof(dp)); sort(a,a+n,cmp);//关键:q-p 小的在前
for(i = ; i<n; i++)
{
for(j = m; j>=a[i].q; j--)//注意:剩余的钱大于q才能买
{
dp[j] = max(dp[j],dp[j-a[i].p]+a[i].v);
}
}
printf("%d\n",dp[m]);
} return ;
}

别人的AC代码

证明:

假设有物品1~n,

1)考虑第1件物品:j 的范围在m~q[1],因为有钱q[1]是能买到物品1的最低前提,那么对dp[m~q[1]]进行更新,结果都是v[1],而dp[0~q[1]]都是0(不包括dp[q[1]])。是正确的。

2)考虑第2件物品:因为状态转移公式是 dp[j] = max(dp[j], dp[j-p[i]] + v[i]),所以疑虑在于式子j-p[2]>=q[2]-p[2]是否成立。根据第2个for的限制条件,可知 j-q[2]>=0,两边各减去p[2],那么j-p[2]>=q[2]-p[2],以上式子成立。可知:j-p[1]是能够满足第2件物品的差额要求的。

3)考虑第i件物品,由于1~i-1这些物品都满足限制条件才会购买,那么第i件物品同样和第2件物品一样的证明。

hdu 3466 Proud Merchants 自豪的商人(01背包,微变形)的更多相关文章

  1. HDU 3466 Proud Merchants 带有限制的01背包问题

    HDU 3466 Proud Merchants 带有限制的01背包问题 题意 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有.最强大的王国.因此,即使他们的国家不再那么富有,这个国 ...

  2. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  3. hdu 3466 Proud Merchants 01背包变形

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  4. HDU 3466 Proud Merchants【贪心 + 01背包】

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  5. hdu 3466 Proud Merchants(有排序的01背包)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  6. HDU 3466 Proud Merchants(01背包问题)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  7. HDU 3466 Proud Merchants(01背包)

    这道题目看出背包非常easy.主要是处理背包的时候须要依照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (J ...

  8. HDU 3466 Proud Merchants(01背包)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  9. HDU 3466 Proud Merchants(背包问题,要好好理解)

    Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...

随机推荐

  1. openstack官方指导书

    openstack官方网站:https://docs.openstack.org/ 由于openstack的官方文档有点多,所以这里对其进行梳理一下 Release Notes 发布版本 新功能,升级 ...

  2. 使用BIND安装智能DNS服务器(一)---基本的主从DNS服务器搭建

    参考网址:http://www.unixmen.com/dns-server-installation-step-by-step-using-centos-6-3/ DNS(Domain Name S ...

  3. 文件解析库doctotext安装和使用

    安装doctotext 1 安装GCC到4.6以上 tar jxf gcc-4.7.0.tar.bz2 cd gcc-4.7.0 编译 ./contrib/download_prerequisites ...

  4. HashMap为什么是线程不安全的

    HashMap底层是一个Entry数组,当发生hash冲突的时候,hashmap是采用链表的方式来解决的,在对应的数组位置存放链表的头结点.对链表而言,新加入的节点会从头结点加入. 我们来分析一下多线 ...

  5. Centos7 使用 supervisor 管理进程

    一.安装 //直接使用pip安装(pip的安装 http://www.cnblogs.com/yxhblogs/p/8971251.html) pip install supervisor 二.配置 ...

  6. Linux 程式減肥(strip & objcopy)(转载)

    转自:http://calamaryshop.blogspot.com/2011/11/linux-strip-objcopy.html 對於設計嵌入式Linux系統的研發人員來說,記憶體的空間是非常 ...

  7. [Xcode 实际操作]七、文件与数据-(12)数据持久化存储框架CoreData的使用:查找CoreData中的数据

    目录:[Swift]Xcode实际操作 本文将演示如何查找数据持久化对象. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //引入数据持 ...

  8. UVALive - 3695 Distant Galaxy

    InputThere are multiple test cases in the input file. Each test case starts with one integer N, (1 ≤ ...

  9. Spring配置文件xsi:schemaLocation无法解析导致启动失败的解决方案

    今天遇到过情况,spring的配置文件在本地读取没有问题,扔到线上服务器运行就报无法解析xml,找了很久问题,发现是因为线上服务器无法上网,导致无法下载相关的xsd文件,没办法不能上网就只有使用本地的 ...

  10. PHP 扩展篇 _ 持续更新

    记住这个网站:http://pecl.php.net/ PHP-Redis扩展更新时间:2019/05/06 PHP安装Redis 1:下载目前最新版的redis插件 wget http://pecl ...