HDU3466 Proud Merchants [背包]
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7536 Accepted Submission(s): 3144
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?
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.
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3
分析:一开始考虑直接01背包,将限制条件直接换成j>=q[i],但是发现这种思路是错的。显然物品的顺序会对其产生影响,那么就要考虑如何排序。然后试了几种排序发现都不对,还是K_lord和five20大佬讲过以后才明白。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<iomanip>
using namespace std;
const int N=5e3+;
int n,m,dp[N];
struct Node{int p,q,v;}a[N];
bool cmp(Node x,Node y)
{return x.q-x.p<y.q-y.p;}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m){
for(int i=;i<=n;i++)
cin>>a[i].p>>a[i].q>>a[i].v;
memset(dp,,sizeof(dp));
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)
for(int j=m;j>=a[i].q;j--)
dp[j]=max(dp[j],dp[j-a[i].p]+a[i].v);
cout<<dp[m]<<"\n";}
return ;
}
HDU3466 Proud Merchants [背包]的更多相关文章
- HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- hdu3466 Proud Merchants(01背包)
https://vjudge.net/problem/HDU-3466 一开始想到了是个排序后的背包,但是排序的策略一直没对. 两个物品1和2,当p1+q2>p2+q1 => q1-p1& ...
- HDU--3466 Proud Merchants (01背包)
题目http://acm.hdu.edu.cn/showproblem.php?pid=3466 分析:这个题目增加了变量q 因此就不能简单是使用01背包了. 网上看到一个证明: 因为如果一个物品是5 ...
- [hdu3466]Proud Merchants
题目描述 Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and po ...
- Proud Merchants(01背包变形)hdu3466
I - Proud Merchants Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Proud Merchants(POJ 3466 01背包+排序)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- Proud Merchants(01背包)
Proud Merchants Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- HDU 3466 Proud Merchants(01背包)
这道题目看出背包非常easy.主要是处理背包的时候须要依照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (J ...
- hdu 3466 Proud Merchants 01背包变形
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
随机推荐
- mongo在centos与windows上部署与配置,及远程连接mongo与数据用户和角色分配
1.下载mongodb社区版: windows 安装包安装: https://www.mongodb.com/download-center#community(mongo下载中心) 配置环境变量 控 ...
- 51Nod 1062 序列中最大的数 | 简单DP
#include "iostream" #include "cstdio" using namespace std; #define LL long long ...
- vijos 1313 金明的预算方案 树形DP
描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今天一 ...
- 【设计模式】 模式PK:代理模式VS装饰模式
1.概述 对于两个模式,首先要说的是,装饰模式就是代理模式的一个特殊应用,两者的共同点是都具有相同的接口,不同点则是代理模式着重对代理过程的控制,而装饰模式则是对类的功能进行加强或减弱,它着重类的功能 ...
- MySQL和Postgresql的区别
一.PostgreSQL相对于MySQL的优势 1.在SQL的标准实现上要比MySQL完善,而且功能实现比较严谨:2.存储过程的功能支持要比MySQL好,具备本地缓存执行计划的能力:3.对表连接支持较 ...
- 【BZOJ4903】【CTSC2017】吉夫特 [DP]
吉夫特 Time Limit: 15 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行一个整数n. 接下 ...
- Java——关于static关键字的那些事总结
前言: 先说说今天为啥要谈这个东西,虽然学Java已经有两年了,但是今天,本着温故而知新的态度,仔细的第三次翻看了<Head Firt Java>这本书,虽然这本书介绍的很多东西都特别基础 ...
- Python阶段复习 - part 4 - 用户登录程序
简易版: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__:Dahlhin import sys userinfo = r'userinf ...
- Linux 入门记录:五、vi、vim 编辑器
一.vi.vim编辑器 vi 是一个命令行界面下的文本编辑工具,最早在 1976 年由 Bill Joy 开发.当时名字叫做 ex.vi 支持绝大多数操作系统(最早在类 Unix 操作系统的 BSD上 ...
- zip函数的应用
#!/usr/bin/env python # encoding: utf-8 from itertools import zip_longest # ➍ # zip并行从输入的各个可迭代对象中获取元 ...