POJ 3624 Charm Bracelet 背包题解

最简单的背包问题了,本题应该除了背包就一个考点了:不能开二维数组。我没开过二维,不过看数据是不可以的。太大了。

做法有两种改进省内存DP:

1 所谓的滚动数组

2 逆向填表

很久没做背包DP,突然觉得这种背包问题很简单了。

下面给出两种解法:

1 calBag()是滚动数组

2 calBag2()是逆向填表

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;

const int MAX_W = 12881;
const int MAX_N = 3403;
int N, M;//M是total weight
int wei[MAX_N];
int desi[MAX_N];
int tbl[MAX_W];

int calBag2()
{
	memset(tbl, 0, sizeof(int)*(M+1));
	for (int i = 1; i <= N; i++)
	{
		for (int j = M; j >= wei[i]; j--)
			tbl[j] = max(tbl[j], tbl[j-wei[i]]+desi[i]);
	}
	return tbl[M];
}

int calBag()
{
	vector<vector<int> > tbl(2, vector<int>(M+1));
	bool id = true;
	for (int i = 1; i <= N; i++)
	{
		for (int j = 1; j < wei[i] && j <= M; j++) tbl[id][j] = tbl[!id][j];
		for (int j = wei[i]; j <= M; j++)
		{
			tbl[id][j] = max(tbl[!id][j], tbl[!id][j-wei[i]]+desi[i]);
		}//注意都是上一列的数据往下拉,都是!id数列比较
		id = !id;
	}
	return tbl[!id][M];
}

int main()
{
	while (scanf("%d %d", &N, &M) != EOF)
	{
		for (int i = 1; i <= N; i++)
		{
			scanf("%d %d", &wei[i], &desi[i]);
		}
		printf("%d\n", calBag2());
	}
	return 0;
}

POJ 3624 Charm Bracelet 背包题解

时间: 2024-10-29 19:07:31

POJ 3624 Charm Bracelet 背包题解的相关文章

poj 3624 Charm Bracelet 背包DP

Charm Bracelet Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (

POJ 3624 Charm Bracelet(背包)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24914   Accepted: 11226 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

[再做01背包] POJ 3624 Charm Bracelet

接触动态规划的第一题是数塔问题,第二题就是01背包问题了. 当时看的懵懵懂懂,回过头来再看这道题还是非常简单的了. 用 dp[i][j] 表示取前i种物品,使它们总体积不超过j的最优取法取得的价值总和状态转移方程:dp[i][j] = max(dp[i-1][j],dp[i-1][j-cost[i]]+weight[i]) 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstri

POJ 3624 Charm Bracelet

Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weightWi (1 ≤ Wi ≤

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

POJ 3624 Charm Bracelet (01背包)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26078   Accepted: 11726 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

POJ 3624 Charm Bracelet (01)(背包入门)

Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi 

POJ 3624 Charm Bracelet(01背包模板)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45191   Accepted: 19318 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

0-1背包问题,附上例题(POJ - 3624 Charm Bracelet)

0-1背包问题的题目样式 有 N 件物品和一个容量为 M 的背包.放入第 i 件物品耗费的费用是 Wi,得到的价值是 Vi.求解将哪些物品装入背包可使价值总和最大. 0-1背包问题关键在于该物品放或不放,即在当前容量为M的的情况下,选择不选择该物品,那么就有一个转移方程 for(i=0  -  N) for(j=0  -  M) dp[i][j] = max(dp[i-1][j],dp[i-1][j+w[i]]+v[i]); 当前物品为i,当前的背包容量为j,如果不选当前该物品,则选取dp[i-