4131:Charm Bracelet
-
-
描述
-
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 iin the supplied list has a weight Wi(1 ≤ Wi≤ 400), a 'desirability' factor Di(1 ≤ Di≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M(1 ≤ M≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
\
-
输入
-
Line 1: Two space-separated integers: N and M
Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: W i and D i -
输出
-
Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
-
样例输入
-
4 6 1 4 2 6 3 12 2 7 -
样例输出
-
23 -
\
-
非常明显的01背包问题,使用动态规划方式解决。发现这个题非常的蛋疼,使用vector就会超时,使用数组就accepted,这个感觉不够科学。具体的介绍可以看我的另外一篇博文:
-
代码如下:
-
#include<iostream> #include<vector> #include<string> #include<map> #include<queue> using namespace std; int main() { int n, m; cin >> n >> m; int d[2][12881]; int things[3405][2]; //vector<vector<int>> d(n, vector<int>(m+1, 0)); // vector<vector<int>> things(n, vector<int>(2, 0)); for (int i = 0; i < n; i++) { int tw, td; cin >> tw >> td; things[i][0] = tw; things[i][1] = td; } for (int i = 0; i < m+1; i++) { if (things[0][0]>i) { d[0][i] = 0; } else d[0][i] = things[0][1]; } for (int i = 1; i < n; i++) { for (int j = 0; j < m + 1; j++) { int tw = things[i][0]; int td = things[i][1]; if (j >= tw) d[i%2][j] = max(d[abs(i % 2 - 1)][j - tw] + td, d[abs(i % 2 - 1)][j]); else d[i%2][j] = d[abs(i % 2 - 1)][j]; } } cout << d[abs(n%2-1)][m] << endl; system("pause"); } -
\
-