-
Notifications
You must be signed in to change notification settings - Fork 2
/
1083 - Histogram(Stack).cpp
48 lines (46 loc) · 1.18 KB
/
1083 - Histogram(Stack).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
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
//freopen("out.txt","w",stdout);
int tc,n;
scanf("%d",&tc);
for(int t=1; t<=tc; t++){
scanf("%d",&n);
int ara[n];
for(int i=0; i<n; i++) scanf("%d",&ara[i]);
ll area = 0;
ll temp=0;
stack<int> st;
for(int i=0; i<n; ){
if(st.empty() || ara[st.top()] <= ara[i] ){
st.push(i++);
}
else{
int a = st.top();
st.pop();
if(st.empty()){
temp = ara[a]*i;
}
else{
temp = ara[a]*(i-st.top()-1);
}
if(temp>area) area = temp;
}
}
while(!st.empty()){
int a = st.top();
st.pop();
if(st.empty()){
temp = ara[a]*n;
}
else{
temp = ara[a]*(n-st.top()-1);
}
if(temp>area) area = temp;
}
printf("Case %d: %lld\n",t,area);
}
return 0;
}