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. Python-学习-项目1-即时标记-1

    买了一本Python入门,奈何看不下去,只能是先看后面的项目,看到那里不懂的时候在回去学习. 项目名字:即时标记 大致的意思就是把一个纯文本文件标记成自己想要的格式文件. 首先就是待处理文本,我找不到 ...

  2. CentOS Linux release 7.5.1804下安装MySQL5.7.24

    1.环境查看: 2.卸载自带MariaDB数据库: 3.下载MySQL5.7.14安装包: 4.使用wget工具下载需要安装数据库的依赖包: 5.解压缩bundel包: 6.按照顺序进行安装: 7.数 ...

  3. [HNOI2004]打鼹鼠

    鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个\(n*n\)的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气.你 ...

  4. 关于2018年东南大学Robomaster算法组工作的总结

    笔者在写作时,为东南大学机器人俱乐部下Robomaster大赛SUPER NOVA战队算法组的负责人之一(这名字写起来好长).而SUPER NOVA战队则于2018年5月19日正式结束了中部分区赛,获 ...

  5. PHP Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

    最近用ThinkPHP,给公司布置线上的网站的时候,遇到的一个问题,记录一下. 打开IE浏览器的设置,Internet选项里的高级,将”显示友好的HTTP错误消息“前都勾去掉! 再次刷新,看到的错误是 ...

  6. BZOJ 3670 NOI2014 动物园 KMP+dp

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意概述:令num[i]表示字符串由1~i的字符形成的前缀中不相重叠的相同前后缀的数 ...

  7. HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)

    Description “Farm Game” is one of the most popular games in online community. In the community each ...

  8. 制作一个简易计算器——基于Android Studio实现

    一个计算器Android程序的源码部分分为主干和细节两部分. 一.主干 1. 主干的构成 计算器的布局 事件(即计算器上的按钮.文本框)监听 实现计算 2. 详细解释 假设我们的项目名为Calcula ...

  9. Mininet实验 基于Mininet测量路径的损耗率

    实验原理 在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为,此外,还可以利用控制器测量路径的损耗率.在本实验中,基于Mininet脚本,设置特定的交换机间的路径损耗速率,然后编 ...

  10. ArcGIS10.2中文版安装和破解教程

    http://jingyan.baidu.com/article/e73e26c0cb5c1324adb6a791.html