标签:表示 获得 make namespace out ems eof sea contains
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
题意:这一题跟01背包很相似只是多了一个限制条件q,题目给力n件物品,手里有m元,每件物品有三个参数 \(p、q、val\)分别表示 价格、期望、价值,如果你想买某个价格为p的物品,那之后手里的钱大于等于q的时候你才可买它,问在手里有m元的情况下最多可以 买到的最大价值是多少。
分析:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define mod int(1e9 + 7) //这里不要忘了加括号
#define rl rt << 1
#define rr rt << 1 | 1
const int mxn = 5005;
struct Item
{
int p, q, val;
int c;
bool operator <(const Item a) const
{
return c < a.c;
}
} item[mxn];
int dp[mxn];
int main()
{
/* freopen("A.txt","r",stdin); */
/* freopen("Ans.txt","w",stdout); */
int n, m;
while(~ scanf("%d %d", &n, &m))
{
for(int i = 1; i <= n; i ++)
scanf("%d %d %d", &item[i].p, &item[i].q, &item[i].val), item[i].c = item[i].q - item[i].p;
sort(item + 1, item +1 + n);
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i ++)
for(int j = m; j >= item[i].q; j --)
dp[j] = max(dp[j], dp[j - item[i].p] + item[i].val);
printf("%d\n", dp[m]);
}
return 0;
}
I - I HDU - 3466Proud Merchants(贪心+01背包)
标签:表示 获得 make namespace out ems eof sea contains
原文地址:https://www.cnblogs.com/lql-nyist/p/12737133.html