Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7095 Accepted Submission(s): 2953
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?
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 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
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU
题意:给你一个n,m表示n个样例,m是总的背包容量,每组样例中给你 pi,qi,vi ,分别表示 pi表示购买的价格,qi表示至少有qi些钱才可以购买此样品,vi表示样品的价值 问你背包装满后的最大价值
分析: 第一次见到这个题我是直接按照价钱排序,从大到小,试了组样例不可以,有转换思路,多加了一维表示价格,又不行,最后看了kuangbin的思路,才恍然大悟,直接对q-p进行排序,然后01背包即可,这样理解,先是贪心的让你赔更少的钱,也就是先考虑这个差最少,然后再依次递推到大的就可以了
参考代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
struct node{
int q,p,v;
bool operator < (const node &x)const{ //重载小于
return (this->q-this->p) < (x.q-x.p);
}
}a[N];
int dp[N];
int main(){
int n,m;
while(cin>>n>>m){
memset(dp,0,sizeof(dp));
for(int i = 0;i < n;i++){
cin>>a[i].p>>a[i].q>>a[i].v;
}
sort(a,a+n);
for(int i = 0;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]<<endl;
}
return 0;
}
- 如有错误或遗漏,请私聊下UP,ths