-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathUVa 11003 - Boxes.cpp
35 lines (32 loc) · 1016 Bytes
/
UVa 11003 - Boxes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static const int MAX_WEIGHT = 3000, MAX_LOAD = 3000;
int main()
{
int N;
while (cin >> N, N != 0)
{
// dp[i] stores the maximum number of boxes that can be
// stacked up for exactly total weight i.
vector<int> W(N + 1), L(N + 1);
for (int i = 1; i <= N; ++i)
cin >> W[i] >> L[i];
vector<int> dp(MAX_WEIGHT + MAX_LOAD + 1, 0);
// Condisder box N, box N-1..to box 1.
for (int box = N; box >= 1; --box)
{
for (int load = L[box]; load >= 0; --load)
{
if (dp[load])
dp[load + W[box]] = max(dp[load + W[box]], // The original best stack.
dp[load] + 1); // The box stack.
}
if (dp[W[box]] == 0)
dp[W[box]] = 1;
}
cout << *max_element(dp.begin(), dp.end()) << endl;
}
return 0;
}