题目
最少能见到的建筑物如下所示
左图:两个橙色的高度一样,如果按最少的算,它们可能是同一个建筑物,但是前面的建筑物比它们高,所以遮住了2-3位置的橙色建筑物,而绿色的比红色的矮,如果绿色的在红色建筑物后面,就会被红色的遮住,从而看不见,所以绿色和红色一定是分开的两座建筑。所以维护一个递增的单调栈,如果比栈顶元素高,就加入,如果比栈顶元素矮,答案加加,把高的弹出,如果一样高度,就不操作。
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define LL long long
#define mem(a, b) memset(a, b, sizeof(a))
#define N 100010
#define MOD
using namespace std;
int n, x;
int main() {
int cas=1;
while(scanf("%d", &n)!=EOF) {
stack<int> st;
int ans=0;
for(int i=1; i<=n; i++) {
scanf("%d", &x);
while(!st.empty() && x<st.top()) {
ans++, st.pop();
}
if(st.empty() || x>st.top())
if(x!=0)
st.push(x);
}
while(!st.empty()) {
ans++;
st.pop();
}
cout<<"Case "<<cas++<<": ";
cout<<ans<<endl;
}
}