题意:

   要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖。给出身上的钱和所有东西的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. boost编译配置及简单使用

    boost编译配置及简单使用 1.下载 http://www.boost.org/ 2.编译: A.解压 boost_1_55_0.zip 到boost路径 B.运行 bootstrap.bat. 会 ...

  2. 2013-2014回首&展望

    2013年,可以说是此前18年中,最重要最感触的一年了. 和老婆相爱,考高考,入大学等等事情全都在这美妙的一年里发生了. 2013: 开心:·和老婆相爱,共同经历了大大小小的风雨·每天都有期待的东西· ...

  3. js学习笔记2:循环和try/catch/throw

    今天学习了js的比较.if/else.switch/case和各种循环,这些东西每种语言都大同小异,没什么好看的,重点说一下js的循环. JavaScript 支持不同类型的循环: for - 循环代 ...

  4. SO_REUSEADDR的分析

    今天协议个socket程序时碰到了这个问题,选自博客http://www.cppblog.com/ace/archive/2006/04/29/6446.html 敲完代码,等下看它.

  5. Depth Buffer

    Up until now there is only one type of output buffer you've made use of, the color buffer. This chap ...

  6. Git 时光穿梭鸡 撤销修改

    工作区内容修改了, 但是并未add到暂存区, 想 回退到上一个版本 在readme.txt中添加了一行: Git is a distributed version control system. Gi ...

  7. Animation Blueprint, Set Custom Variables Via C++

    https://wiki.unrealengine.com/Animation_Blueprint,_Set_Custom_Variables_Via_C%2B%2B Animation Bluepr ...

  8. uoj#352. 新年的五维几何(概率期望+爆搜)

    传送门 我还以为这是个五维半平面交呢--结果没看数据范围-- 题解 //minamoto #include<bits/stdc++.h> #define R register #defin ...

  9. React入门看这篇就够了

    摘要: 很多值得了解的细节. 原文:React入门看这篇就够了 作者:Random Fundebug经授权转载,版权归原作者所有. React 背景介绍 React 入门实例教程 React 起源于 ...

  10. according to tld or attribute directive in tag file attribute *** does not accept any expressions

    http://stackoverflow.com/questions/13428788/according-to-tld-or-attribute-directive-in-tag-file-attr ...