hdu Proud Merchants

简介:

Proud Merchants

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 84 Accepted Submission(s): 46
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
这一题是一个背包问题。比较关键的是:为了尽可能的使取得最大Vi时背包装得较满,
我们要对“Qi与Pi的差”从大到小排序。比如下面这个实例:
4 20
5 15 20
8 12 19
2 16 21
9 11 13
如果不排序,直接进行动态规划的话
2 16 21这个物品相当于没有放入背包,因为假如你先选择前两种物品的话,
2 16 21就会因为16这个条件不能与前两种物品同时放入背包,但是如果提前排好序就不一样了。
这一点处理好,这一题基本就没问题了。
复制代码
 1 #include <iostream>
2 #include <stdio.h>
3 #include <algorithm>
4 #include <string.h>
5 using namespace std;
6 int f[5001];
7 typedef struct
8 {
9 int p,q,v;
10 }item;
11 bool cmp(item a,item b)
12 {
13 return a.q-a.p<b.q-b.p;
14 }
15 int main()
16 {
17 int n,m;
18 item it[501];
19 while(scanf("%d%d",&n,&m)!=EOF)
20 {
21 for(int i = 0; i < n; i++)
22 {
23 scanf("%d%d%d",&it[i].p,&it[i].q,&it[i].v);
24 }
25 sort(it,it+n,cmp);
26 memset(f,0,sizeof(f));
27 for(int i = 0; i < n; i++)
28 for(int j = m; j>=it[i].q;j--)
29 {
30 if(f[j-it[i].p]+it[i].v>f[j])
31 f[j] = f[j-it[i].p]+it[i].v;
32 }
33 printf("%d\n",f[m]);
34 }
35 return 0;
36 }
复制代码














相关文章
|
1月前
|
Java
hdu-2546-饭卡
hdu-2546-饭卡
15 0
|
7月前
|
Java
hdu 2503 a/b + c/d
hdu 2503 a/b + c/d
27 0
|
算法 Java
HDU 2084 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
152 0
HDU2203亲和串
博客水平见水平......目前阶段就是这么菜,我会好好努力的!毕业直接拿到阿里offer!
1209 0
|
定位技术
hdu 4771 Stealing Harry Potter's Precious
点击打开链接 题意:题目给定一个n*m的地图,地图有一个起点标记为'@',还有'#'表示不能够走的,'.'表示可以走。给定k个点,问从起点开始把这k个点走过去的最小步数。
774 0
hdu 1892 See you~
点击打开hdu 1892 思路: 二维树状数组 分析: 1 题目给定4种操作:  S x1 y1 x2 y2 询问以(x1 , y1) - (x2 , y2)为对角线的矩形的面积,但是这个对角线不一定是正对角线。
998 0
|
C++ Java
HDU1880
题意就是根据咒语查功能,根据功能查看是否存在相应咒语,题意简单,不过是道不错的练习题。         下面的都MLE了,听说C++用G++提交才可以AC,否则也MLE;方法很多,不想做了……         方法一:我用Java的HashMap一直MLE,即便由value反查key减少映射数也一样MLE,听说C++的map可以AC。
1059 0