-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathferryLoading.cpp
109 lines (97 loc) · 2.44 KB
/
ferryLoading.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
102
103
104
105
106
107
108
109
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("ferryLoading.in");
ofstream fout("ferryLoading.out");
int main()
{
int testCase;
fin >> testCase;
for (int c = 1; c <= testCase; ++c)
{
if (c != 1)
{
fout << '\n';
}
int l, in = -1;
fin >> l;
l *= 100;
vector<int> a = {0}, sum = {0};
while (true)
{
fin >> in;
if (in == 0)
{
break;
}
a.push_back(in);
sum.push_back(sum.back() + in);
}
int size0 = a.size() - 1;
if (size0 == 0)
{
fout << "0\n";
continue;
}
int maxCount = 0, maxLength = 0;
vector<vector<bool>> dp(size0 + 1, vector<bool>(l + 1, false));
vector<vector<int>> store(size0 + 1, vector<int>(l + 1, 0));
dp[0][0] = true;
for (int i = 1; i <= size0; ++i)
{
for (int j = 0; j <= l; ++j)
{
if (j >= a[i] && dp[i - 1][j - a[i]] == 1)
{
maxCount = i;
maxLength = j;
dp[i][j] = true;
store[i][j] = j - a[i];
}
else if (sum[i] - j <= l && dp[i - 1][j] == 1)
{
maxCount = i;
maxLength = j;
dp[i][j] = true;
store[i][j] = j;
}
}
}
vector<bool> out(maxCount + 1);
int temp;
for (int i = maxCount; i >= 1; --i)
{
temp = store[i][maxLength];
if (temp == maxLength)
{
out[i] = false;
}
else
{
out[i] = true;
}
maxLength = temp;
}
fout << maxCount << '\n';
for (int i = 1; i <= maxCount; ++i)
{
if (out[i] == false)
{
fout << "port" << '\n';
}
else
{
fout << "starboard" << '\n';
}
}
}
return 0;
}