-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10261.cpp
101 lines (77 loc) · 2.15 KB
/
uva-10261.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//uva 10261
//Ferry Loading
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <stack>
using namespace std;
int main(void)
{
int T = 0;
cin >> T;
while (T--) {
int l = 0;
cin >> l;
l *= 100;//in centimeters
vector <int> nums;//in centimeters
while (1){
int n;
cin >> n;
if (n == 0)
break;
nums.push_back(n);
}
vector < vector <bool> > arr(nums.size() + 1, vector <bool> (l + 1, 0));
vector < vector <int> > count(nums.size() + 1, vector <int> (l + 1, -1));// for back tracking
arr[0][l] = 1;
int sum = 0;
int car = 0;
int i = 1;
for (; i <= nums.size(); ++i){
bool carInserted = 0;
for (int j = 0; j <= l; ++j){
if (arr[i - 1][j]) {
if (j - nums[i - 1] >= 0){//inserting left
arr[i][j - nums[i - 1]] = 1;
carInserted = 1;
count[i][j - nums[i - 1]] = j;
}
int remainRight = (l - (sum - (l - j)));
if (remainRight - nums[i - 1] >= 0){//inserting right
arr[i][j] = 1;
carInserted = 1;
count[i][j] = j;
}
}
}
sum += nums[i - 1];
if (!carInserted)
break;
}
car = i - 1;
stack <string> Stack;
int start = -1;
for (int i = 0; i < arr[car].size(); ++i)
if (arr[car][i]){
start = i;
break;
}
cout << car << endl;
while(car > 0){
if (count[car][start] == start)
Stack.push("port");
else
Stack.push("starboard");
start = count[car][start];
--car;
}
while (!Stack.empty()){
cout << Stack.top() << endl;
Stack.pop();
}
if (T != 0)
cout << endl;
}
return 0;
}