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++学习013多态

    何为多态 面向对象最要的特征之一就是多态,而纯虚函数是实现多态的主要方式.它可以提供一个通过用的接口,同样调用一个方法, 由于运算对象不同,方法也不同,这也就是所谓的动态绑定. #include &l ...

  2. Laravel 5.5 创建全局公共函数

    一.需求 我在使用 Laravel 进行项目逻辑处理的时候要加载一些方法,需要全局调用 这个方法又必须得是一个全局函数,因此需要给 Laravel 创建全局的公共函数 二.实现 1.创建文件 在 ap ...

  3. JavaSE复习(二)集合

    Collection List(存取有序,有索引,可以重复) ArrayList 底层是数组实现的,线程不安全,查找和修改快,增和删比较慢 LinkedList 底层是链表实现的,线程不安全,增和删比 ...

  4. Spring Boot学习(一):入门篇

    目录 Spring Boot简介 Spring Boot快速搭建 1 新建项目 2 运行项目 3 设置spring boot可以热部署(修改后端代码后,自动部署,不用手动部署) 3.1:配置pom.x ...

  5. Graphic的一些基本概念

    做了张很丑陋的图,估计还不准确...先凑合看吧~

  6. [剑指Offer] 15.反转链表

    题目描述 输入一个链表,反转链表后,输出链表的所有元素. [思路1]三个指针在链表上同时滑动. /* struct ListNode { int val; struct ListNode *next; ...

  7. Java IO 小结

    Java IO 的学习需要明白流设计的体系结构,这样才能在实际需要的时候,通过API文档查阅,快速实现功能.

  8. oracle 引用类型声明

  9. 【题解】ZOJ1420 Cashier Employment

    论文——冯威<浅析差分约束系统>. 论文讲得很详细,就不解释了.主要想记录一下对于差分约束的理解(感觉以前的学习真的是在囫囵吞枣啊……) 差分约束系统,同于解决线性的不等关系是否存在合法解 ...

  10. [洛谷P2986][USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目大意:给你一棵树,每个点有点权,边有边权,求一个点,使得其他所有点到这个点的距离和最短,输出这个距离 题解:树形$DP$,思路清晰,转移显然 卡点:无 C++ Code: #include < ...