接雨水(單調棧)
阿新 • 來源:網路 • 發佈:2021-05-31
【問題描述】
給定 n 個非負整數表示每個寬度為 1 的柱子的高度圖,計算按此排列的柱子,下雨之後能接多少雨水。例如,當給定數字序列為 0,1,0,2,1,0,1,3,2,1,2,1 時,柱子高度圖如下所示,最多可以接 6 個單位的雨水。

【輸入格式】
第一行包含整數 n。
第二行包含 n 個非負整數。
【輸出格式】
輸出一個整數,表示最大接水量。
【資料範圍】
1≤n≤100000,
序列中元素均不大於 1000。
【輸入樣例】
12
0 1 0 2 1 0 1 3 2 1 2 1
【輸出樣例】
6
【演算法程式碼】
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int h[maxn]; //obstacle height
int stk[maxn]; //monotonous stack
int n;
int main() {
cin>>n;
for(int i=0; i<n; i++)
cin>>h[i];
int ans=0;
int top=-1; //top:pointer of stack top
//stk[top]:obstacle number
//h[stk[top]] represents the height of the obstacle with number stk[top]
//Before deleting a shorter obstacle,
//calculate its water storage relative to the previous obstacle
//The rainwater capacity between two obstacles=
//the height difference between two obstacles*
//the distance between two obstacles
for(int i=0; i<n; i++) {
int last=0;
while(top>=0 && h[stk[top]]<h[i]) {
ans+=(h[stk[top]]-last)*(i-stk[top]-1);
last=h[stk[top]];
top--;
}
if(top>=0) ans+=(h[i]-last)*(i-stk[top]-1);
stk[++top]=i;
}
cout<<ans<<endl;
return 0;
}
/*
12
0 1 0 2 1 0 1 3 2 1 2 1
6
*/
【參考文獻】
https://blog.csdn.net/liudongshizhang/article/details/108034437
https://www.acwing.com/problem/content/1576/
