http://acm.hdu.edu.cn/showproblem.php?pid=3466

题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有钱M时的最大价值。

一看这个题,就觉得直接按p背包还是按q背包都不对,然后就没有然后了。。。

然后看了题解:是说按q-p贪心,其实是这样,每次取q-p最小的,那么每次留下的自然就是最多的金钱。

至于严格的证明。。。。。。。待研究

剩下的就是01背包了。。,。。

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a)) typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct NODE
{
int p,q;
int val;
}item[MAXN];
int N, M, DP[MAXM]; int cmp(NODE a, NODE b)
{
return a.q-a.p < b.q-b.p;
} int main()
{
while(~scanf("%d %d", &N, &M))
{
mem0(DP);
for(int i=;i<N;i++)scanf("%d %d %d", &item[i].p, &item[i].q, &item[i].val);
sort(item, item+N, cmp);
for(int i=;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 ;
}

最新文章

  1. thrift笔记
  2. MAC OS 系统使用心得
  3. hihoCoder1284机会渺茫(唯一分解定理 + 约分)
  4. JS 时分秒验证
  5. javascript数组详解
  6. MVC-处理时间格式
  7. iOS开发常用国外网站清单
  8. 使用MyEclipse生成实体类和Hibernate映射文件
  9. Linux下C程序的存储空间布局
  10. 关于JWPlayer播放器的一些测试学习
  11. 理解JavaScript中的作用域
  12. Java-每日编程练习题②(数组练习)
  13. Docker 核心技术之容器
  14. T-SQL_select语句详解
  15. Python学习笔记七
  16. Python3.6 提示 ModuleNotFoundError: No module named &#39;_ssl&#39; 模块问题
  17. Razor视图基本语法
  18. 一个封装不错的 TcpClient 类
  19. Linux块设备IO子系统(一) _驱动模型
  20. android sdk 汉化

热门文章

  1. 漫游Kafka设计篇之消息传输的事务定义
  2. postgresql之数据字典
  3. C# 创建系统服务并定时执行【转载】
  4. Android如何获取系统高度、标题栏和状态栏高度
  5. DESCryptoServiceProvider加密、解密
  6. lseek()函数
  7. Java中HashMap的数据结构
  8. ubuntu 上更新安装 openoffice.org3的过程
  9. Linux共享内存(一)
  10. poj 2409(polya定理模板)