hdu3466 Proud Merchants
来源:互联网 发布:python beginwith 编辑:程序博客网 时间:2024/05/24 07:39
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 1010 15 105 10 53 105 10 53 5 62 7 3
Sample Output
511
这题比较难想,想了很久看别人题解做出来了。这题关键是要理解怎么安排加入背包的物品的顺序,才能使后面加入的物品不影响前面已经加入的物品。我发现每一次进行循环的时候,都是从qi开始循环的,而且要用到qi-pi及其后的数,那么为了不使后面的物品影响前面的,要保证qi-pi是递增的,这样后面的就不会与前面冲突,可以仔细想想:).
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct node{int p,q,v,cha;}a[600];int f[6000];bool cmp(node a,node b){return a.cha<b.cha;}int max(int a,int b){return a>b?a:b;}int main(){int n,m,i,j;while(scanf("%d%d",&n,&m)!=EOF){memset(a,0,sizeof(a));memset(f,0,sizeof(f));for(i=1;i<=n;i++){scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);a[i].cha=a[i].q-a[i].p;}sort(a+1,a+1+n,cmp);for(i=1;i<=n;i++){for(j=m;j>=a[i].p;j--){if(j>=a[i].q){f[j]=max(f[j],f[j-a[i].p]+a[i].v);}}}printf("%d\n",f[m]);}return 0;}
0 0
- hdu3466 Proud Merchants
- hdu3466-Proud Merchants
- hdu3466 Proud Merchants
- HDU3466 Proud Merchants
- hdu3466 Proud Merchants
- 【精华】【HDU3466】Proud Merchants
- HDU3466---Proud Merchants
- hdu3466 Proud Merchants
- 【HDU3466】【Proud Merchants】
- Hdu3466 Proud Merchants
- hdu3466-Proud Merchants
- Hdu3466 Proud Merchants
- 【HDU3466】Proud Merchants
- HDU3466 Proud Merchants
- hdu3466 Proud Merchants
- HDU3466 Proud Merchants
- hdu3466 Proud Merchants
- HDU3466:Proud Merchants(01背包)
- EasyUI回车提交表单
- Substring with Concatenation of All Words 字符串操作 匹配的字串数
- jQuery插件开发模式
- 说说实际项目中如何防止重复提交
- 导出Excel时出现错误 java.lang.IllegalStateException: getOutputStream() has already been called
- hdu3466 Proud Merchants
- myeclipse10配置maven
- ng-repeat指令中使用track by子语句解决数据重复遍历的错误
- jar命令成功完成 java -jar 命令却提示“没有主清单属性”!
- 字符串-03 字符串的冒泡排序(20)
- Appium 并发和主从远程控制
- iOS 开发学习40 自动布局约束
- 如何打造139团队(不同层次人员的选择与培养,大型研发团队,大型敏捷开发团队)
- 【SuperMap .Net 组件】示例代码的用法