-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa10261_Ferry_loading.cpp
86 lines (82 loc) · 2.16 KB
/
UVa10261_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;
bool dp[215][10001];
int main()
{
int cases;
cin >> cases;
while (cases--)
{
int length, tmp, carAmount = 1;
int carlength[201];
cin >> length;
length *= 100;
while (cin >> tmp && tmp)
{
carlength[carAmount++] = tmp;
}
dp[0][0] = true;
// 用來確認還能不能放
int onFerryLength = 0;
// 用來找結果
int amountMax = 0, lengthMax = 0;
for (int n = 1; n < carAmount; n++)
{
onFerryLength += carlength[n];
for (int l = 0; l <= length; l++)
{
if (onFerryLength - l <= length && dp[n - 1][l])
{
amountMax = n;
lengthMax = l;
dp[n][l] = true;
}
if ((l + carlength[n]) <= length && dp[n - 1][l])
{
amountMax = n;
lengthMax = l + carlength[n];
dp[n][lengthMax] = true;
}
}
}
bool result[amountMax + 1];
int clearLength = lengthMax;
cout << amountMax << endl;
for (int i = amountMax; i > 0; i--)
{
if (dp[i - 1][lengthMax])
{
result[i] = false;
}
else
{
result[i] = true;
lengthMax -= carlength[i];
}
}
for (int i = 1; i <= amountMax; i++)
{
if (result[i])
{
cout << "port" << endl;
}
else
{
cout << "starboard" << endl;
}
}
if (cases)
{
// 前面代表停幾輛,後面則是長度
for (int i = 0; i <= amountMax; i++)
{
for (int j = 0; j <= clearLength; j++)
{
dp[i][j] = false;
}
}
cout << endl;
}
}
return 0;
}