forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestrectangle.cpp
49 lines (46 loc) · 1.01 KB
/
largestrectangle.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
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T>
T largestrectangle(vector<T> &v){
int n=v.size();
T res=0;
using P = pair<int, T>;
stack<P> sp;
sp.emplace(-1,T(0));
for(int i=0;i<n;i++){
int j=i;
while(sp.top().second>v[i]){
j=sp.top().first;
chmax(res,(i-j)*sp.top().second);
sp.pop();
}
if(sp.top().second<v[i]) sp.emplace(j,v[i]);
}
while(!sp.empty()){
chmax(res,(n-sp.top().first)*sp.top().second);
sp.pop();
}
return res;
}
//END CUT HERE
//INSERT ABOVE HERE
signed DPL_3_C(){
Int n;
cin>>n;
vector<Int> v(n);
for(Int i=0;i<n;i++) cin>>v[i];
cout<<largestrectangle(v)<<endl;
return 0;
}
/*
verified on 2018/06/11
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_3_C&lang=jp
*/
signed main(){
DPL_3_C();
return 0;
}