-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastaways.cpp
101 lines (95 loc) · 2.13 KB
/
Castaways.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
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const int MAX = 257;
int N, rect, tri, s1, s2, max_rect, max_tri, arr[MAX], sorted[MAX];
int best_rect_area() {
for (int k = MAX-1; k > 0;) {
if (sorted[k] > 1) {
if (s1 == -1) {
s1 = k;
sorted[s1] -= 2;
} else {
s2 = k;
sorted[s1] += 2;
return s1 * s2;
}
} else {
k--;
}
}
if (s1 != -1)
sorted[s1] += 2;
s1 = s2 = -1;
return 0;
}
bool is_triangle(int i, int j, int k) {
return i + j > k && i + k > j && j + k > i;
}
int tri_area(int i, int j, int k) {
double s = (i + j + k) / 2.0;
return (int) sqrt(s * (s-i) * (s-j) * (s-k));
}
void try_best(int i, int j, int k) {
if (is_triangle(i, j, k)) {
sorted[k]--;
s1 = s2 = -1;
rect = best_rect_area();
tri = tri_area(i, j, k);
if (tri + rect > max_tri + max_rect) {
max_tri = tri;
max_rect = rect;
}
sorted[k]++;
}
}
int main() {
while (cin >> N) {
fill_n(sorted, MAX, 0);
fill_n(arr, MAX, 0);
// read and sort data
for (int i = 0; i < N; i++) {
cin >> arr[i];
sorted[arr[i]]++;
}
// get best answer for every tuple
s1 = s2 = -1;
max_tri = 0; max_rect = best_rect_area();
for (int i = 1; i < MAX; i++) {
if (sorted[i] > 0) {
sorted[i]--; // start using side
for (int j = i; j < MAX; j++) {
if (sorted[j] > 0) {
sorted[j]--; // start using side
// get best rectangle info
s1 = s2 = -1;
rect = best_rect_area();
if (rect > 0)
sorted[s1] -= 2; sorted[s2] -= 2;
// get best area without rect sides (s1, s2)
for (int k = MAX-1; k >= j; k--) {
if (sorted[k] > 0 && is_triangle(i, j, k)) {
tri = tri_area(i, j, k);
if (tri + rect > max_tri + max_rect) {
max_tri = tri;
max_rect = rect;
}
}
}
// try to get best with s1 or s2
if (rect > 0) {
sorted[s1] += 2; sorted[s2] += 2;
int tmp_s1 = s1, tmp_s2 = s2;
try_best(i, j, tmp_s1);
try_best(i, j, tmp_s2);
}
sorted[j]++; // stop using side
}
}
sorted[i]++; // stop using side
}
}
cout << max_tri + max_rect << endl;
}
}