题目描述:

Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more. The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi. If he had M units of money, what’s the maximum value iSea could get?

输入描述:

There are several test cases in the input.

Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money. Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.

The input terminates by end of file marker.

输出描述:

For each test case, output one integer, indicating maximum value iSea could get.

题意:

有n个商人,每个商人都很自负,如果你的钱数少于Qi,那么就不与你交易

思路:

这题交易的顺序会影响答案,比如

5 10 5

3 5 6

如果先去第一个再去第二个可以获得11

但是先去第二个再去第一个就只能获得6

先买i后买j

M >= Qi, M - Pi >= Qj

买j后不能买i

M >= Qj, M - Pj < Qi

推导:

Pi - M <= -Qj

M - Pj < Qi

Pi - Pj < Qi - Qj

Pi - Qi < Pj - Qj

Qi - Pi > Qj - Pj

如果满足Qi - Pi > Qj - Pj那么就说明i 不能先j

排序后背包即可

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
ll dp[5005];
struct node{int p, q, v;
}arr[505];
bool cmp(node a, node b){return a.q - a.p < b.q - b.p;
}
int main(){int n, m;while(cin >> n >> m){for(int i = 1; i <= n; i++){cin >> arr[i].p >> arr[i].q >> arr[i].v;}sort(arr + 1, arr + 1 + n, cmp);for(int j = 0; j <= m; j++){dp[j] = 0;}for(int i = 1; i <= n; i++){for(int j = m; j >= arr[i].q; j--){dp[j] = max(dp[j], dp[j - arr[i].p] + arr[i].v);}}cout << dp[m] << endl;}return 0;
}

HDU3466 Proud Merchants相关推荐

  1. 【HDU - 3466 】Proud Merchants(dp,背包问题,巧妙排序)

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

  2. 01背包入门:HDU 3466 Proud Merchants

    这道题对我来说还是有点难度的,所以参考了两个博主的博客:博主1博主2 Problem Description Recently, iSea went to an ancient country. Fo ...

  3. 【HDU 3466】【Proud Merchants】

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

  4. Proud Merchants

    题目链接: https://vjudge.net/contest/348156#problem/I 题面: 翻译: 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有和最强大的王国.因此 ...

  5. 这段时间做的简单dp题目(部分)

    这些时间vj上做的部分题目 HDU5115 题意:第一行t,t组测试数据,每组数据第一行输入n表示n匹狼,第二行给出一个序列表示每匹狼的伤害,第三行给出每匹狼能给周围狼的伤害增幅,求怎样打可以得到最小 ...

  6. 暑假N天乐 —— 01背包及变形

    暑假集训的第一天 第二天了. 决定还是先把基础巩固一下好了,万一后面训练赛出事故更尴尬. 所以今天的主题是 -- 01背包及变形 动态规划概念 动态规划(Dynamic programming,DP) ...

  7. 提高组精英班Day4

    动态规划 动态规划 (dynamic programming, DP) 是求解决策过程最优化的数学方法. 动态规划是通过拆分问题,定义问题状态和状态之间的关系,使得问题能够以递推的方式去解决. 如何拆 ...

  8. android jar 无法访问r文件,android项目中gen目录不能自动生成R.java的原因

    php分页代码简单实现 版权声明:本文为博主原创文章,未经博主允许不得转载. 数据库操作类代码:mysqli.func.php <?php // 数据库连接常量 define('DB_HOST' ...

  9. 2013-2014集训第二次个人积分赛

    地址:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6346 密码acmore 这次比赛实在有点寒酸,11题我就弄出个最简单的题(还是找规 ...

最新文章

  1. AI Debate2 | 李飞飞提「AI北极星」,卡尼曼指「系统 1」理解有误
  2. Loading class `com.mysql.jdbc.Driver'. This is dep
  3. 回首2018 | 分析型数据库AnalyticDB:不忘初心 砥砺前行...
  4. 基础004_V7-DSP Slice
  5. C# datagridview、datagrid、GridControl增加行号
  6. 干货|MIT线性代数课程精细笔记[第二课]
  7. Currency Exchange (POJ 1860)
  8. remote connect openshift mysql
  9. 读《scikiit-learn机器学习》黄永昌第四章
  10. 区块链ppt资料准备
  11. php网站背景颜色代码,html如何设置背景颜色?
  12. 硬盘格式化了怎么恢复数据
  13. 海德汉 LSV2 协议采集 2
  14. 第四次机考(2019) C. f1二
  15. 五个最好的压缩软件下载
  16. MATLAB最速下降法求解函数极小值
  17. 解决chrome自动填充白色背景(input:-internal-autofill-previewed)问题
  18. K8s使用Ceph纠删码池做持久化卷
  19. 嵌入式-ARM-学习总结(7):按键与中断
  20. 【Datawhale跨模态实践学习笔记】项目后端学习

热门文章

  1. 不小心永久删除的文件怎么恢复?被永久删除的文件还能找回吗
  2. JavaWeb简单案例——文件的上传
  3. 一个游戏大量合服代表什么_合区的游戏下载_合区的游戏大全_手游排行榜_网游/单机游戏_九游...
  4. 地质学与计算机的联系,计算机与地质学
  5. SpringBoot中多数据源的配置
  6. Android中启动,分享,卸载应用程序
  7. 斯蒂文斯理工学院计算机科学硕士,美国斯蒂文斯理工学院计算机科学硕士专业.pdf...
  8. 商城 goods.php id,拼多多-多多进宝 只有goods_id没有goods_sign如何获取商品详情,拼多多goods_sign...
  9. C#接入腾讯云OCR照片识别的软件更新
  10. 汽车BCM车身控制器测试系统解决方案