POJ 3624 Charm Bracelet (01背包 + 内存优化)

#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,01背包,charm,bracelet,DP动态规划,3624)