-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHISTOGRAM.cpp
65 lines (52 loc) · 1.07 KB
/
HISTOGRAM.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
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
const ll N = 1e5+3;
ll hist[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(1)
{
ll n;
cin>>n;
if(n==0)
{
return 0;
}
for(ll i=0;i<n;i++)
{
cin>>hist[i];
}
ll ar=0;
ll max_ar=0;
ll i=0;
ll tp;
stack<ll> st;
while(i<n)
{
if(st.empty() || hist[st.top()]<=hist[i])
{
st.push(i);
i++;
}
else{
tp = st.top();
st.pop();
ar = hist[tp]*((st.empty())?i:i-st.top()-1);
max_ar = max(ar,max_ar);
}
}
while(!(st.empty()))
{
tp = st.top();
st.pop();
ar = hist[tp]*((st.empty())?i:i-st.top()-1);
max_ar = max(ar,max_ar);
}
cout<<max_ar<<endl;
}
}