Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1899 Accepted Submission(s): 762
Problem DescriptionRecently, 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?
InputThere 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.
OutputFor each test case, output one integer, indicating maximum value iSea could get.
Sample Input
2 1010 15 105 10 53 105 10 53 5 62 7 3
Sample Output
511
題意:商品的價格是Pi, 你的錢要大於等於Qi才可以購買,購買商品所或得的價值Vi
背包問題:公式 dp[j]=dp[j]=Math.max(dp[j],dp[j-k1]+k2);
import java.io.*;import java.util.*;public class Main {int n,m,M=6000;int dp[]=new int[M];public static void main(String[] args) {new Main().work();}void work(){Scanner sc=new Scanner(new BufferedInputStream(System.in));while(sc.hasNext()){n=sc.nextInt();m=sc.nextInt();Node node[]=new Node[n];for(int i=0;i<n;i++){int a=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt();node[i]=new Node(a,b,c);}Arrays.sort(node);//每件商品按 (this.qi-this.pi)>(o.qi-o.pi) 進行升序排序Arrays.fill(dp,0);for(int i=1;i<=n;i++){for(int j=m;j>=0;j--){if(j>=node[i-1].qi){int k1=node[i-1].pi;int k2=node[i-1].vi;dp[j]=Math.max(dp[j],dp[j-k1]+k2);}}}System.out.println(dp[m]);}}class Node implements Comparable<Node>{int pi;int qi;int vi;Node(int pi,int qi,int vi){this.pi=pi;this.qi=qi;this.vi=vi;}public int compareTo(Node o) {return (this.qi-this.pi)>(o.qi-o.pi)?1:-1;}}}