HDU3466 Proud Merchants[背包DP 条件限制]

Proud Merchants

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 5599    Accepted Submission(s): 2362

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元,物品价格p,价值q,条件是手里多于q元才能买,求最大价值


按q-p小到大排序然后01背包

f[i][j]是前i个物品j元,只有j>qi时才能买第i个物品

所以考虑第i个物品,普通情况下最小可以从f[0]更新来,现在是f[qi-pi],也就是说qi-pi被浪费了

这个浪费当然越小越好

A:p1,q1   B: p2,q2,先选A,则至少需要p1+q2的容量,而先选B则至少需要p2+q1,如果p1+q2>p2+q1,那么要选两个的话的就要先选A再选B,公式可换成q1-p1 < q2-p2,


首先发现qi-pi小,不可能有比qi-pi更小的qj,使得可以从j到i

这样可以保证后来的j-pi大于前面的q-p,也就是更新来的范围递增

就按照取出两个比较的套路来行了

//
//  main.cpp
//  hdu3466
//
//  Created by Candy on 29/10/2016.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=505,M=5005;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();}
    return x*f;
}
int n,m;
struct item{
    int p,q,w;
    bool operator <(const item &r)const{return q-p<r.q-r.p;}
}a[N];
int f[M];
void dp(){
    memset(f,0,sizeof(f));
    for(int i=1;i<=n;i++)
        for(int j=m;j>=a[i].q;j--)
            f[j]=max(f[j],f[j-a[i].p]+a[i].w);
}
int main(int argc, const char * argv[]){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++){a[i].p=read();a[i].q=read();a[i].w=read();}
        sort(a+1,a+1+n);
        dp();
        printf("%d\n",f[m]);
    }
    return 0;
}
时间: 2024-10-26 01:28:21

HDU3466 Proud Merchants[背包DP 条件限制]的相关文章

01背包水题篇之HDU3466——Proud Merchants

这是个好题,菜鸟刚学dp,这题把我以前的想法全都给完完全全的颠覆了.其实是自己没了解无后效性的概念. 然后我去开开心心滴跑去问队长:"队长,队长,怎么理解动归的无后效性啊???" 学长很深沉滴对我说:"做多了就会了" "噢噢"(好吧) 然后学长又补了句:"能构成有向无环图的都能用DP搞." 我心里想:"队长就知道搞妹~~~." 默默去翻小白书看看DAG去了. 为了搞清楚这题怎么写,操了度娘千百遍,还是没搞定

hdu 3466 Proud Merchants &lt;背包+sort排序&gt;

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3616    Accepted Submission(s): 1511 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

hdu-3466 Proud Merchants(01背包之转移)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3466 题目很容易理解,但是如何将其转换为01背包是个问题. 对物品按 qi-pi 的值从小到大排序,因为这样可以保证每次更新的状态值从小到大递增,前面更新过的状态不会影响后面更新的状态. 题目代码: 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <algorithm&g

hdu 3466 Proud Merchants(0-1背包+排序)

题目来源:hdu 3466 Proud Merchants Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3595 Accepted Submission(s): 1500 Problem Description Recently, iSea went to an ancient country. For

hdu 3466 Proud Merchants(有排序的01背包)

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4039    Accepted Submission(s): 1677 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

HDU 3466 Proud Merchants(01背包)

这道题目看出背包很容易,主要是处理背包的时候需要按照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2674    Accepted Submission(s): 1109 Problem Description Recently, iSea we

HDU3466 背包DP

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 5566    Accepted Submission(s): 2345 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

Proud Merchants(01背包)

Proud Merchants Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 12   Accepted Submission(s) : 5 Problem Description Recently, iSea went to an ancient country. For such a long time, it was the mo

Proud Merchants HDU - 3466 01背包&amp;&amp;贪心

最近,我去了一个古老的国家.在很长一段时间里,它是世界上最富有.最强大的王国.结果,这个国家的人民仍然非常自豪,即使他们的国家不再那么富有.商人是最典型的,他们每个人只卖一件商品,价格是Pi,但是如果你的钱少于Qi,他们就会拒绝和你交易,而我评估每件商品的价值Vi.如果他有M单位的钱,iSea能得到的最大值是多少? 输入 在输入中有几个测试用例.每个测试用例以两个整数N M(1≤N≤500,1≤M≤5000)开始,表示项目编号和初始资金.接着N行,每一行包含3个数字Pi, Qi和Vi(1≤Pi≤