博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ_2392 Space Elevator(多重背包)
阅读量:7235 次
发布时间:2019-06-29

本文共 1456 字,大约阅读时间需要 4 分钟。

  /*多重背包问题。把a[i]排下序,然后把a[i]作为选第i件物品时的背包容量V。最后结果要[0, max(a[i])]遍历一遍,找到最大值。*/ //My Code: #include 
#include
#include
#include
using namespace std; const int N = 40007; const int M = 407; class node {
public: int h, c, a; friend bool operator < (const node& t1, const node& t2) {
return t1.a < t2.a; } }t[M]; int f[N]; int main() {
//freopen("data.in", "r", stdin); int k, i, ans, j, m; int h1, V, c1; while(~scanf("%d", &k)) {
memset(t, 0, sizeof(t)); h1 = V = c1 = 0; for(i = 0; i < k; i++) {
scanf("%d%d%d", &t[i].h, &t[i].a, &t[i].c); if(t[i].h > h1) h1 = t[i].h; if(t[i].a > V) V = t[i].a; if(t[i].c > c1) c1 = t[i].c; } sort(t, t + k); memset(f, 0, sizeof(f)); for(i = 0; i < k; i++) {
if(t[i].c*t[i].h > t[i].a) {
for(j = t[i].h; j <= t[i].a; j++) {
f[j] = max(f[j], f[j-t[i].h] + t[i].h); } continue; } m = 1; while(t[i].c) {
if(m > t[i].c) m = t[i].c; t[i].c -= m; for(j = t[i].a; j >= m*t[i].h; j--) {
f[j] = max(f[j], f[j-m*t[i].h] + m*t[i].h); } m <<= 1; } } ans = 0; for(i = 0; i <= V; i++) {
ans = ans < f[i] ? f[i] : ans; } cout << ans << endl; } return 0; } //ps: 2011年最后一天, Happy New Year ^_^

转载地址:http://wylfm.baihongyu.com/

你可能感兴趣的文章
OSG开源教程(转)
查看>>
一个缓存实现平均分配队列的方案
查看>>
How do I extract a single column from a data.frame as a data.frame
查看>>
Js获取后台集合List的值和下标的方法
查看>>
Jenkins~powershell+cmd发布nuget包包
查看>>
网络上的等待事件 —— SQL*Net message from client/dblink
查看>>
Myeclipse、eclipse安装lombok
查看>>
C# AES要解密的数据的长度无效
查看>>
JS 推断URL中是否含有 http:// 假设没有则自己主动为URL加上
查看>>
基于ELK5.1(ElasticSearch, Logstash, Kibana)的一次整合
查看>>
利用recv和readn函数实现readline函数
查看>>
MacOs brew 命令行安装常见工具
查看>>
XDroidMvp 轻量级的Android MVP快速开发框架
查看>>
学习项目管理
查看>>
Android 非静态内部类导致内存泄漏原因深入剖析
查看>>
java zxing生成二维码
查看>>
Nginx安装lua-nginx-module模块
查看>>
elasticsearch 工具类
查看>>
【转】Eclipse 乱码 解决方案总结(UTF8 -- GBK)
查看>>
揭示同步块索引(上):从lock开始
查看>>