日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

单调栈3_水到极致的题 HDOJ4252

發(fā)布時(shí)間:2023/12/1 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单调栈3_水到极致的题 HDOJ4252 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

A Famous City

題目大意 給出正視圖? 每一列為樓的高度 最少有幾座樓

坑點(diǎn) 樓高度可以為0 代表沒(méi)有樓

貢獻(xiàn)了兩發(fā)RE 原因 if(!s.empty()&&tem){s.push(tem); continue;}并不能篩去 空棧且 tem為0的情況

改為 if(!s.empty()){if(tem) s.push(tem); continue;} 后AC

題目思路 維護(hù)一個(gè)單調(diào)遞增的棧? 假如新加入的樓高度小于top元素 那我們知道top元素一定是單獨(dú)的一棟樓 我們就pop掉 ans++

如果 等于top 那么可以認(rèn)為 這兩個(gè)是一棟樓(最少)

如果大于pop? 就添加下一個(gè)

操作結(jié)束后 剩余在棧內(nèi)的元素 每一個(gè)必然是獨(dú)立的一棟樓

樓的高度0 一定要特判

對(duì)棧進(jìn)行top pop 這些操作前一定要判empty啊

代碼如下

#include<cstdio> #include<map> //#include<bits/stdc++.h> #include<vector> #include<stack> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<cstdlib> #include<climits> #define PI acos(-1.0) #define INF 0x3f3f3f3f using namespace std; typedef long long ll; typedef __int64 int64; const ll mood=1e9+7; const int64 Mod=998244353; const double eps=1e-9; const int N=2e7+10; const int MAXN=1e5+5; inline void rl(ll&num){num=0;ll f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();num*=f; } inline void ri(int &num){num=0;int f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();num*=f; } int getnum()//相鄰的個(gè)位整數(shù)輸入 如想分別保存1234 輸入連續(xù)的1234 a[i]=getnum();就可以實(shí)現(xiàn) {char ch=getchar();while((ch<'0' || ch>'9') && ch!='-')ch=getchar();return (ch-'0'); } inline void out(int x){ if(x<0) {putchar('-'); x*=-1;}if(x>9) out(x/10); putchar(x%10+'0'); } int main() {int n,ci=0;while(scanf("%d",&n)!=EOF){int tem;ll ans=0;stack<int>s;for(int i=0;i<n;i++){ri(tem);if(s.empty()){if(tem)s.push(tem);continue;}if(s.top()==tem) continue;if(tem<s.top()){while(!s.empty()&&s.top()>tem){s.pop();ans++;}if(!s.empty()&&s.top()==tem) continue;else{if(tem)s.push(tem);}}else{if(tem)s.push(tem);}}printf("Case %d: ",++ci);ans+=s.size();cout<<ans<<endl;}return 0; } AC代碼

?

轉(zhuǎn)載于:https://www.cnblogs.com/Geek-xiyang/p/5444885.html

總結(jié)

以上是生活随笔為你收集整理的单调栈3_水到极致的题 HDOJ4252的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。