POJ 3624 Charm Bracelet (01背包 + 内存优化)
2015-10-12 19:55
381 查看
#include <stdio.h> #define MAX_CHARMS 3402 #define MAX_WEIGHT 12880 #define MAX(x, y) ( (x) > (y) ? (x) : (y) ) int weight[MAX_CHARMS + 1]; int desirability[MAX_CHARMS + 1]; int numOfCharms; int weightLimit; //要写成优化内存版本,不然会内存超限 int maxDesirability[MAX_WEIGHT + 1]; int main(){ scanf("%d%d", &numOfCharms, &weightLimit); int charm; for (charm = 1; charm <= numOfCharms; charm++) scanf("%d%d", &weight[charm], &desirability[charm]); int preCharms, lowerWeight, totalWeight; for (preCharms = 1; preCharms <= numOfCharms; preCharms++){ lowerWeight = weight[preCharms]; for (totalWeight = weightLimit; totalWeight >= lowerWeight; totalWeight--) maxDesirability[totalWeight] = MAX( maxDesirability[totalWeight], maxDesirability[ totalWeight - lowerWeight ] + desirability[preCharms]); } printf("%d\n", maxDesirability[weightLimit]); }
相关文章推荐
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板
- poj 2485 Highways