-
Notifications
You must be signed in to change notification settings - Fork 0
/
fence.cc
57 lines (48 loc) · 1.28 KB
/
fence.cc
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
#include <algorithm>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::max;
using std::min;
using std::vector;
vector<int> fence;
void input() {
int fence_count;
cin >> fence_count;
fence.clear();
for (int i = 0; i < fence_count; ++i) {
int fence_height;
cin >> fence_height;
fence.push_back(fence_height);
}
}
int solve(int left, int right) {
if (left == right) return fence[left];
int mid = (left + right) / 2;
int result = max(solve(left, mid), solve(mid + 1, right));
int lo = mid, hi = mid + 1;
int height = min(fence[lo], fence[hi]);
result = max(result, height * 2);
while (left < lo || hi < right) {
if (hi < right && (lo == left || fence[lo - 1] < fence[hi + 1])) {
++hi;
height = min(height, fence[hi]);
} else {
--lo;
height = min(height, fence[lo]);
}
result = max(result, height * (hi - lo + 1));
}
return result;
}
int main() {
int test_cases;
cin >> test_cases;
for (int i = 0; i < test_cases; ++i) {
input();
int result = solve(0, fence.size() - 1);
cout << result << '\n';
}
return 0;
}