/
// question 2 //
/
//一个小偷夜晚潜入去博物馆里面偷画
//博物馆共有N幅画, 每幅画的重量为w_i, 每幅画的价格为v_i
//但是小偷的包只能装下最多C的重量, 请问小偷最多能收获多少钱
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
// helper function
int max(int a, int b) { return (a > b) ? a : b; }
// c: capacity of theft's bag
// W: array of weight of each art
// V: array of value of each art
int MuseumTheft(int c, int W[], int V[])
{
vector<int> ans(c+1, 0);
for(int i = 0; i<3; i++){
for(int j = c; j>=1; j--){
//如果想要减小空间复杂度就需要逆向遍历重量,因为前序遍历会造成前面的值更改而影响后面更新的值
if(j>=W[i]){
ans[j] = max(ans[j], ans[j-W[i]]+V[i]);
}
}
}
int max_value = ans.back();
return max_value;
}
int main()
{
int V[] = { 60, 100, 120 };
int W[] = { 10, 40, 50 };
int c = 50;
cout << MuseumTheft(c, W, V);
return 0;
}
背包问题