题目链接: 传送门

Proud Merchants

Time Limit: 1000MS     Memory Limit: 65536K

Description

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?

Input

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.

Output

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

Sample Iutput

2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3

Sample Output

5
11

解题思路

题目大意:有n个商品m块钱,给出买每个商品所花费的钱P、钱包里需要至少Q才有资格买、商品的价值V。问你如何购买商品的价值最大。

考虑下面的例子:A:p1=5, q1=10, v1=5; B:p2=3, q2=5, v2=6; 如果先买物品A再买物品B的话我至少需要10元钱,也就是money >= p1+q2,如果先买物品B再买物品A的话,我们至少需要13元钱,也就是money >= p2+q1。得到相同的价值的物品,我们却需要两个不同价值的钱,当然我们选择要花少的钱购买相同价值的商品了!

所以应使p1+q2 < p2+q1,交换位置p1-q1 < p2 - q2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; struct Node{
int p,q,v;
}; bool cmp(Node x,Node y)
{
return (x.q - x.p) < (y.q - y.p);
} int main()
{
int N,M;
while (~scanf("%d%d",&N,&M))
{
Node node[505];
int dp[5005];
memset(dp,0,sizeof(dp));
memset(node,0,sizeof(node));
for (int i = 0;i < N;i++)
{
scanf("%d%d%d",&node[i].p,&node[i].q,&node[i].v);
}
sort(node,node+N,cmp);
for(int i = 0;i < N;i++)
{
for (int j = M;j >= node[i].q;j--)
{
dp[j] = max(dp[j-node[i].p] + node[i].v,dp[j]);
}
}
printf("%d\n",dp[M]);
}
return 0;
}

HDU 3466 Proud Merchants(01背包)的更多相关文章

  1. hdu 3466 Proud Merchants 01背包变形

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

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

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

  3. HDU 3466 Proud Merchants 排序 背包

    题意:物品有三个属性,价格p,解锁钱数下线q(手中余额>=q才有机会购买该商品),价值v.钱数为m,问购买到物品价值和最大. 思路:首先是个01背包问题,但购买物品受限所以应先排序.考虑相邻两个 ...

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

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

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

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

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

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

  7. HDU 3466 Proud Merchants(01背包)

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

  8. hdu 3466 Proud Merchants 自豪的商人(01背包,微变形)

    题意: 要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖.给出身上的钱和所有东西的3个属性,求最大总价值. 思路: 1)WA思路:与01背包差不多,d ...

  9. Proud Merchants(01背包)

    Proud Merchants Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

随机推荐

  1. [Codeforces 163D]Large Refrigerator (DFS+剪枝)

    [Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...

  2. 爬虫之Js混淆&加密案例

    需求: 中国空气质量在线监测分析平台是一个收录全国各大城市天气数据的网站,包括温度.湿度.PM 2.5.AQI 等数据,链接为:https://www.aqistudy.cn/html/city_de ...

  3. Stupid cat & Doge (分形图)

    [题目描述] [题目链接] http://noi.openjudge.cn/ch0204/8463/ [算法] 为求等级N下的点的坐标可由几何关系找到其与等级N-1下对应点的关系,然后递归直至所有点的 ...

  4. 75.Binary Tree Maximum Path Sum(二叉树的最大路径和)

    Level:   Hard 题目描述: Given a non-empty binary tree, find the maximum path sum. For this problem, a pa ...

  5. Ioc和DI之间的关系(依赖注入的核心概念)

    1.开篇闲话 由于之前做的很多项目都没接触到这个,后来到了另一个公司,他们的代码结构是基于领域驱动设计的,其中里面的对象都是通过依赖注入方式(Sprint.NET)实现的,也大致了解了哈,在网上搜了些 ...

  6. CSS语法规则

    一.At-rule 一种以@开头的声明语句,以分号;结尾.语法规则为: @IDENTIFIER (RULE); . At-rule主要用作表示CSS的行为,参考: https://www.cnblog ...

  7. rpmcache - 缓存 RPM 打包头部

    SYNOPSIS rpmcache [ PACKAGE_NAME ... ] DESCRIPTION rpmcache 遍历文件树,可能通过 FTP 使用远程文件,使用 glob(7) 表达式过滤路径 ...

  8. smbsh - 允许用UNIX命令访问NT文件系统

    总览 smbsh 描述 此程序是Samba套件的一部分. smbsh允许你用UNIX命令诸如ls,egrep和rcp等来访问NT文件系统.必须用动态链接的shell以便使smbsh工作正常. 从命令提 ...

  9. mysql02---客户端与服务器模型

    目录 一.客户端与服务器模型 连接MySQL方式 总结: 二.MySQL服务器构成 三.MySQL的结构 一.客户端与服务器模型 1.mysql是一个典型的C/S服务结构 1.1 mysql自带的客户 ...

  10. 关于如何测试cpu性能的命令操作 linux系统

    for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/d ...