Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3556 Accepted Submission(s): 1478
Problem 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?
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.
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 Input
2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3
Sample Output
5
11
Author
iSea @ WHU
Source
Recommend
贪心 + 01背包
只是这个贪心的思想我领悟了好久的说,我自个做了好久,却发现不管我按那个排都不合适啊,这是为何啊?
后来我就看了题解,难怪我弄得不对呢,排序竟然是按p - q排的,简单证明如下:
假设有A、B两个物品,A的花费为p1, 限制金额为q1,B的花费为p2,,限制金额为q2。那么有两种情况:
1、先A后B所需空间:p1 + q2
2、先B后A所需空间:p2 + q1
对本题而言,若想在进行一个物品的购买时他所需的前面的状态都已更新过,那么需要所需空间大的在前面,即 p1 + q2 > p2 + q1则A在前面,整理得 :
p1 - q1 > p2 - q2
eg:对于p1 = 5, q1 = 10; p2 = 3, q2 = 5;
V1 = p1 + q2 = 10;
V2 = p2 + q1 = 13;
在进行d[10] = max(d[10], d[10 - 5] + v(第一个物品的价值))即d[5]应该已经更新过,则应先2后1。
接下来就是简单的01背包了。
#include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define ll long long #define _cle(m, a) memset(m, a, sizeof(m)) #define repu(i, a, b) for(int i = a; i < b; i++) #define repd(i, a, b) for(int i = b; i >= a; i--) #define sfi(n) scanf("%d", &n) #define sfl(n) scanf("%I64d", &n) #define pfi(n) printf("%d\n", n) #define pfl(n) printf("%I64d\n", n) #define MAXN 5005 int dp[MAXN]; struct P{ int p, q, v; bool operator < (const P& t) const { return p - q > t.p - t.q; } }b[MAXN]; int main() { int n, m; while(~scanf("%d%d", &n, &m)) { repu(i, 0, n) sfi(b[i].p), sfi(b[i].q), sfi(b[i].v); _cle(dp, 0); sort(b, b + n); repu(i, 0, n) for(int j = m; j >= b[i].q && j >= b[i].p; j--) dp[j] = max(dp[j], dp[j - b[i].p] + b[i].v); pfi(dp[m]); } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 深度学习基础理论————CV中常用Backbone(Resnet/Unet/Vit系列/多模态系列等