Proud Merchants
Time limit:2000/1000 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 4500 Accepted Submission (s): 1873
Problem descriptionrecently, ISea went to an ancient country. For such a long time, it is the most wealthy and powerful kingdom in the world. As a result, the people in this country is still very proud even if their nation hasn ' t been so wealthy any more.
The merchants were the most typical, each of the them only sold exactly one item, the price is Pi, but they would refuse to M Ake a trade with the 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?
Inputthere is several test cases in the input.
Each test case begin with the integers N, M (1≤n≤500, 1≤m≤5000), indicating the items ' number and the initial mone Y.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1≤pi≤qi≤100, 1≤vi≤1000), their meaning is I n the description.
The input terminates by end of file marker.
Outputfor each test case, output one integer, indicating maximum value ISea could get.
Sample Input2 1010 15 105 10 53 105 10 53 5 62 7 3
Sample Output511
Authorisea @ whu
Source acm-icpc multi-university Training Contest (3)--host by WHU test instructions: givenMYuan Money, thereNItems, the price isWi , Value for Pi , the restriction is that the money in the hand is less than Qi can not buy the item. Maximum value that can be obtained. Exercises
Because this topic increases the purchase precondition, and the ordinary 01 knapsack is a little different;
where is it different? The different place lies in the ordinary 01 knapsack, the purchase order does not affect its result;
But here, we can clearly see that the order of purchase will affect our final result.
so we should determine a correct order of purchase; and then we can think about it, if you decide to buy or not,
realize the maximization of value, you will in what order successively judge;
Obviously, you will first Judge Q big, p small items buy not buy, right, because you value the most, you should try to judge Q large, p small items;
knowing this, the topic is basically solved;
We can sort the data and sort it directly with the difference of p,q .
But what we need to note here is that we want to sort the difference from small to large, not from big to small,
Think about the DP process, j=m,j--; after getting the number in front of the judge , so as to let our data update not be affected;
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5#include <stack>6#include <map>7#include <Set>8#include <algorithm>9 #definell __int64Ten #definePi ACOs (-1.0) One #defineMoD 1 A #defineMAXN 10000 - using namespacestd; - intn,m; the structnode - { - intp,q,v; -}n[505]; + intdp[5005]; - BOOLcmpstructNode AA,structnode BB) + { A if((AA.Q-AA.P) < (bb.q-bb.p)) at return true; - return false; - } - intMain () - { - while(SCANF ("%d%d", &n,&m)! =EOF) in { -Memset (DP,0,sizeof(DP)); tomemset (N,0,sizeof(N)); + for(intI=1; i<=n;i++) -scanf" %d%d%d",&n[i].p,&n[i].q,&n[i].v); theSort (n+1, n+1+n,cmp); * for(intI=1; i<=n;i++) $ {Panax Notoginseng for(intl=m;l>=n[i].q;l--) -Dp[l]=max (dp[l],dp[l-n[i].p]+n[i].v); the } +cout<<dp[m]<<Endl; A } the return 0; +}
HDU 3446 Greedy Thoughts of 01 backpack