-
Notifications
You must be signed in to change notification settings - Fork 0
/
1017_Greed.cpp
110 lines (107 loc) · 3.64 KB
/
1017_Greed.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
110
#include <iostream>
void greed_search(int* packages, const int length, int* result) {
int num_package_2 = 0;
for (int i = length - 1; i >= 0 ; i--) {
if (!packages[i]) continue;
(*result) ++;
switch (i + 1) {
case 6:
packages[5] --;
return greed_search(packages, length, result);
case 5:
if (packages[0] <= 11) packages[0] = 0;
else packages[0] -= 11;
packages[4] --;
return greed_search(packages, length, result);
case 4:
if (packages[1] <= 5) {
if (packages[0] <= 20 - packages[1] * 2) packages[0] = 0;
else packages[0] -= 20 - packages[1] * 2;
packages[1] = 0;
} else {
packages[1] -= 5;
}
packages[3] --;
return greed_search(packages, length, result);
case 3:
if (packages[2] == 1) {
num_package_2 = packages[1];
if (packages[1] <= 5) {
packages[1] = 0;
} else {
packages[1] -= 5;
}
if (packages[0] <= 27 - (num_package_2 > 5 ? 5 : num_package_2) * 4) {
packages[0] = 0;
} else {
packages[0] -= 27 - (num_package_2 > 5 ? 5 : num_package_2) * 4;
}
packages[2] = 0;
} else if (packages[2] == 2) {
num_package_2 = packages[1];
if (packages[1] <= 3) {
packages[1] = 0;
} else {
packages[1] -= 3;
}
if (packages[0] <= 18 - (num_package_2 > 3 ? 3 : num_package_2) * 4) {
packages[0] = 0;
} else {
packages[0] -= 18 - (num_package_2 > 3 ? 3 : num_package_2) * 4;
}
packages[2] = 0;
} else if (packages[2] == 3) {
num_package_2 = packages[1];
if (packages[1] <= 1) {
packages[1] = 0;
} else {
packages[1] -= 1;
}
if (packages[0] <= 9 - (num_package_2 > 1 ? 1 : num_package_2) * 4) {
packages[0] = 0;
} else {
packages[0] -= 9 - (num_package_2 > 1 ? 1 : num_package_2) * 4;
}
packages[2] = 0;
} else {
packages[2] -= 4;
}
return greed_search(packages, length, result);
case 2:
num_package_2 = packages[1];
if (packages[1] < 9) {
packages[1] = 0;
} else {
packages[1] -= 9;
}
if (packages[0] < 36 - 4 * (num_package_2 > 9 ? 9 : num_package_2)) {
packages[0] = 0;
} else {
packages[0] -= 36 - 4 * (num_package_2 > 9 ? 9 : num_package_2);
}
return greed_search(packages, length, result);
case 1:
if (packages[0] <= 36) {
packages[0] = 0;
} else {
packages[0] -= 36;
return greed_search(packages, length, result);
}
}
}
}
int main() {
int packages[6];
while (true) {
int sum = 0;
for (int i = 0; i < 6; i++) {
std::cin >> packages[i];
sum += packages[i];
}
if (!sum) { break; }
int result = 0;
greed_search(packages, 6, &result);
std::cout << result << std::endl;
}
return 0;
}