多重背包问题 POJ 2392
2014-04-16 23:42
295 查看
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct block { int height; int maxHeight; int number; bool operator < (const block& b)const { if(maxHeight < b.maxHeight) return true; return false; } }; block bList[410]; int numList[410][40010]; int mHeight = 0; int realHeight = 0; void completePack(int type,int length) { for (int i=length; i<=bList[type].maxHeight; i++) { numList[type][i] = max(numList[type][i],numList[type][i-length]+length); if(realHeight < numList[type][i]) realHeight = numList[type][i]; } } void zeroOnePack(int type,int length,int blockNumber) { for (int i=bList[type].maxHeight; i>=length*blockNumber; i--) { numList[type][i] = max(numList[type][i],numList[type][i-length*blockNumber]+length*blockNumber); if(realHeight < numList[type][i]) realHeight = numList[type][i]; } } void multiplePack(int type,int length,int maxHeight,int blockNumber) { for (int i=1; i<=mHeight; i++) numList[type][i] = numList[type-1][i]; if(length * blockNumber >= mHeight) { completePack(type,length); } int k = 1; while(k < blockNumber) { zeroOnePack(type,length,k); blockNumber -= k; k = k * 2; } zeroOnePack(type,length,blockNumber); } int main() { int blockNum = 0; while(~scanf("%d",&blockNum)) { memset(numList,0,sizeof(numList)); for (int i=1; i<=blockNum; i++) scanf("%d%d%d",&bList[i].height,&bList[i].maxHeight,&bList[i].number); sort(bList+1,bList+1+blockNum); mHeight = bList[blockNum].maxHeight; for (int i=1; i<=blockNum; i++) multiplePack(i,bList[i].height,bList[i].maxHeight,bList[i].number); printf("%d\n",realHeight); } return 0; }
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- poj 2485 Highways
- poj 1511 Invitation Cards
- poj1042
- poj1080
- poj1088
- poj1063 Flip and Shift
- Android 不同分辨率下如何使字体大小随之变化
- poj 3249 Test for Job 最长路
- HDU4758 AC自动机+DP (HDU4758与HDU2222)
- [LeetCode] Distinct Subsequences
- [LeetCode] Interleaving String
- [LeetCode] Unique Binary Search Tree