-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFillTheTank.cpp
More file actions
69 lines (53 loc) · 1.06 KB
/
FillTheTank.cpp
File metadata and controls
69 lines (53 loc) · 1.06 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
// problem: "https://www.hackerrank.com/contests/practice-contest-iet/challenges/fill-the-tank-1"
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll maxWater(vector<ll>&water)
{
int i,x;
stack<int>S;
S.push(0);
ll ans = 0;
for(i=1;i!=water.size();i++)
{
if(water[S.top()] > water[i])
S.push(i);
else
{
while((!S.empty()) && water[S.top()]<=water[i])
{
x = S.top();
S.pop();
}
if(S.empty())
ans += min(water[i],water[x])*(i-x);
S.push(i);
}
}
if(S.size()==1)
return ans;
while(!S.empty())
{
x = S.top();
S.pop();
if(S.empty())
return ans;
ans += water[x]*(x-S.top());
}
return ans;
}
int main()
{
int n;
cin>>n;
int i;
vector<ll>Water;
for(i=0;i!=n;i++)
{
ll x;
cin>>x;
Water.push_back(x);
}
cout<<maxWater(Water); // define
return 0;
}