-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistogram.cpp
More file actions
58 lines (41 loc) · 1.05 KB
/
Histogram.cpp
File metadata and controls
58 lines (41 loc) · 1.05 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
#include <bits/stdc++.h>
#include <fstream>
#include <ctime>
#include <chrono>
using namespace std;
long long arr[10000000];
int main()
{
long long n;
cin>>n;
for(long long i=0;i<n;i++)
cin>>arr[i];
long long max_area=-1;
stack<long long>s;
long long i=0;
while(i<n)
{
if(s.empty()||arr[s.top()]<=arr[i])
{
s.push(i++);
}
else
{
long long top_index=s.top();
s.pop();
long long currarea=arr[top_index]*(s.empty()?i:i-1-s.top());
if(currarea>max_area)
max_area=currarea;
}
}
while(!s.empty())
{
long long top_index=s.top();
s.pop();
long long currarea=arr[top_index]*(s.empty()?i:i-1-s.top());
if(currarea>max_area)
max_area=currarea;
}
cout<<max_area<<endl;
return 0;
}