-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1725.cpp
53 lines (40 loc) · 1.13 KB
/
1725.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
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long lld;
lld N, h[101010];
lld findIntersection(lld start, lld mid, lld end){
lld i=mid, j=mid+1, minHeight=min(h[i], h[j]), width=2, maxArea=width*minHeight;
while(i>start && j<end){
if(h[i-1] < h[j+1]) minHeight = min(minHeight, h[++j]);
else minHeight = min(minHeight, h[--i]);
maxArea = max(maxArea, ++width*minHeight);
}
while(i>start){
minHeight = min(minHeight, h[--i]);
maxArea = max(maxArea, ++width*minHeight);
}
while(j<end){
minHeight = min(minHeight, h[++j]);
maxArea = max(maxArea, ++width*minHeight);
}
return maxArea;
}
lld solve(lld start, lld end){
if(start == end) return h[start];
lld mid = (start+end)/2;
lld area1 = solve(start, mid);
lld area2 = solve(mid+1, end);
lld area3 = findIntersection(start, mid, end);
return max(area1, max(area2, area3));
}
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
cin >> N;
for(int i=0; i<N; i++){
cin >> h[i];
}
cout << solve(0, N-1);
}