您的位置:首页 > 其它

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]);
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息