forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 10261 - Ferry Loading.cpp
67 lines (65 loc) · 2.08 KB
/
UVa 10261 - Ferry Loading.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int T;
cin >> T;
while ( T-- )
{
// Ferry
int ferry;
cin >> ferry;
ferry *= 100;
int car;
vector<int> cars(1, 0);
// sum[i] stores the total length of cars 1 to i.
vector<int> sum(1, 0);
while (cin >> car, car > 0)
{
cars.push_back(car);
sum.push_back(sum.back() + car);
}
// dp[i][j] is true if cars 1 to i could be loaded for length j on right.
vector<vector<bool> > dp(cars.size(), vector<bool>(ferry + 1, false));
vector<vector<string> > lane(dp.size(), vector<string>(dp[0].size(), ""));
pair<int, int> memo = make_pair(0, 0);
dp[0][0] = true;
for (int i = 1; i < (int)cars.size(); ++i)
for (int j = 0; j <= ferry; ++j)
{
// If cars 1..i-1 were successfully loaded for length j on right.
if (dp[i - 1][j])
{
// Try to load car i on right.
if (ferry - j >= cars[i])
{
dp[i][j + cars[i]] = true;
lane[i][j + cars[i]] = "starboard" ;
memo = make_pair(i, j + cars[i]);
}
// Try to load car i on left.
if (ferry - (sum[i - 1] - j) >= cars[i])
{
dp[i][j] = true;
lane[i][j] = "port";
memo = make_pair(i, j);
}
}
}
cout << memo.first << endl;
string print;
while (lane[memo.first][memo.second] != "")
{
print = lane[memo.first][memo.second] + "\n" + print;
memo.second -= lane[memo.first][memo.second] == "starboard"?
cars[memo.first] : 0;
--memo.first;
}
cout << print;
if (T)
cout << endl;
}
return 0;
}