Proud Merchants

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

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
 
题意:n件商品,每件价格pi,每件商品只有在你钱>=qi才能买入,每件价值vi。现在你有m元钱,问能购买到的商品最大价值和。
 
很好的一道题,刷完这道题对01背包和dp的后效性有了更深理解。思路就是先尝试买qi-pi大的商品,即按照qi-pi从大到小排序,但实际编码过程中需要从小到大排序。为什么呢?考虑在求dp(i, j)时,dp(i, j) = max(dp(i-1,j), dp(i-1, j-q[i]) + v[i]),仔细分析下dp(i-1, j-q[i]) + v[i] 这种情况,发现其实是先买了第i种商品,就是说排在后面的商品其实是先买的。
 
/*
ID: LinKArftc
PROG: 3466.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int maxm = ; struct Node {
int cost, need, val;
} node[maxn];
int dp[maxm];
int n, m; bool cmp(Node a, Node b) {
return (a.need - a.cost) < (b.need - b.cost);
} int main() {
//input;
while (~scanf("%d %d", &n, &m)) {
memset(dp, , sizeof(dp));
for (int i = ; i <= n; i ++) scanf("%d %d %d", &node[i].cost, &node[i].need, &node[i].val);
sort (node+, node+n+, cmp);
for (int i = ; i <= n; i ++) {
for (int j = m; j >= node[i].need; j --) {
dp[j] = max(dp[j-node[i].cost] + node[i].val, dp[j]);
}
}
printf("%d\n", dp[m]);
} return ;
}

POJ3466(01背包变形)的更多相关文章

  1. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  2. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  3. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  5. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  6. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  7. POJ 3211 Washing Cloths(01背包变形)

    Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...

  8. HDU 2955 Robberies(01背包变形)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

随机推荐

  1. C++学习006-条件运算符

    这里我也理解的不咋的,大致意思应该就是根据运算符号 的优先级不同来解决的 条件运算符是其中一部分,而条件运算符具有右结合性,当一个表达式中出现多个条件运算符时,应该将位于最右边的问号与理他最近的冒号配 ...

  2. 容器基础(八): 使用docker swarm部署程序

    环境 基于上一节的env/server:v0.1, env/worker:v0.1镜像, 在基于debian8.2的两台机器上测试部署docker swarm. docker service部署 ➜ ...

  3. c#执行插入sql 时,报错:异常信息:超时时间已到。在操作完成之前超时时间已过或服务器未响应

    问题:c#执行插入sql 时,报错:异常信息:超时时间已到.在操作完成之前超时时间已过或服务器未响应 解决: SqlCommand cmd = new SqlCommand(); cmd.Comman ...

  4. sqlserver 找出字符第N次出现的位置

    [1编写函数]CREATE FUNCTION IndexOf(@str VARCHAR(500),@value VARCHAR(50),@posIndex INT)RETURNS int AS BEG ...

  5. Mapper的方式总结

    Mapper的方式总结: <mappers> <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 --> <package name=&quo ...

  6. 修改maven远程仓库为阿里的maven仓库(复制)

    maven之一:maven安装和eclipse集成 maven作为一个项目构建工具,在开发的过程中很受欢迎,可以帮助管理项目中的bao依赖问题,另外它的很多功能都极大的减少了开发的难度,下面来介绍ma ...

  7. sql between and 边界问题

    1.不同的数据库对 BETWEEN...AND 操作符的处理方式是有差异的.需要自己测试 2.一般情况下.SQL Server中 between and是包括边界值的,not between不包括边界 ...

  8. Android Studio 添加模块依赖

    原文地址: http://fanjiajia.cn/2018/09/27/Android%20Studio%20%E6%B7%BB%E5%8A%A0%E6%A8%A1%E5%9D%97%E4%BE%9 ...

  9. C# Lambda表达式使用累加器例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lamb ...

  10. setcookie函数

    在任何请求的服务器响应都会有个头部,默认情况下,头部发送动作会在第一个输出发生时触发,如echo,<html>.(注:php有个header方法手动发送原生header) 由于setcoo ...