-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargest rectangle.cpp
More file actions
101 lines (84 loc) · 1.74 KB
/
Copy pathLargest rectangle.cpp
File metadata and controls
101 lines (84 loc) · 1.74 KB
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 <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef struct node{
int key, val;
int f, l, r;
bool operator < (node t) const
{
return key < t.key;
}
} node;
vector<node> T;
vector<int> stack;
int top;
int create(int n)
{
top = 1;
stack.push_back(1);
for(int i = 2; i <= n; i++)
{
while(top > 0 && T[i].val < T[stack[top]].val)
top --;
if(top > 0) // 右链中的节点
{
T[i].f = stack[top];
T[i].l = T[stack[top]].r;
T[T[stack[top]].r].f = i;
T[stack[top]].r = i;
if(top == stack.size() - 1)
{
top ++;
stack.push_back(i);
}
else
{
stack[++top] = i;
}
}
else // 根节点
{
T[stack[1]].f = i;
T[i].l = stack[1];
stack[++top] = i;
}
}
return stack[1];
}
int solve(int r, int & ans)
{
int temp = 1;
if(T[r].l)
temp += solve(T[r].l, ans);
if(T[r].r)
temp += solve(T[r].r, ans);
int ans2 = temp * T[r].val;
if(ans2 > ans)
ans = ans2;
// cout << T[r].key << " " << temp << endl;
return temp;
}
int main()
{
int n;
scanf("%d", &n);
T.clear();
stack.clear();
node flag = {0, 0, 0, 0, 0};
T.push_back(flag);
stack.push_back(0);
for(int i = 1; i <= n; i++)
{
node temp = {0, i, 0, 0, 0};
scanf("%d", &temp.val);
T.push_back(temp);
}
sort(T.begin(), T.end());
int root = create(n);
int ans = 0;
solve(root, ans);
cout << ans << endl;
return 0;
}