-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest_rectangle.cpp
More file actions
61 lines (47 loc) · 916 Bytes
/
largest_rectangle.cpp
File metadata and controls
61 lines (47 loc) · 916 Bytes
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
#include "largest_rectangle.hpp"
using namespace std;
struct Building {
Building(int from, int to, int h) : from(from), to(to), h(h) {}
int from;
int to;
int h;
};
void test_largest_rectangle()
{
int n;
cin >> n;
stack<Building> s;
long max_area = 0;
int prev_h = 0;
s.push(Building(0, 0, 0));
for (int i = 0; i <= n; i++) {
int h = 0;
if (i < n) cin >> h;
if (h > prev_h) {
s.push(Building(i, i + 1, h));
} else if (h < prev_h) {
while (!s.empty()) {
Building &b = s.top();
if (b.h > h) {
long area = (i - b.from)*b.h;
max_area = max(max_area, area);
s.pop();
} else if (b.h < h) {
s.push(Building(b.to, i + 1, h));
break;
} else {
b.to = i + 1;
break;
}
}
}
prev_h = h;
}
cout << max_area << endl;
}