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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3250 】Bad Hair Day (单调栈)

發布時間:2023/12/10 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3250 】Bad Hair Day (单调栈) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Some of Farmer John's?N?cows (1 ≤?N?≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads.

Each cow?i?has a specified height?hi?(1 ≤?hi?≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow?i?can see the tops of the heads of cows in front of her (namely cows?i+1,?i+2, and so on), for as long as these cows are strictly shorter than cow?i.

Consider this example:

??????? = =?????? = =?? -?? =???????? Cows facing right --> =?? =?? = = - = = = = = = = = = 1 2 3 4 5 6

Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow's hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow's hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!

Let?ci?denote the number of cows whose hairstyle is visible from cow?i; please compute the sum of?c1?through?cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

Input

Line 1: The number of cows,?N.?
Lines 2..N+1: Line?i+1 contains a single integer that is the height of cow?i.

Output

Line 1: A single integer that is the sum of?c?1?through?cN.

Sample Input

6 10 3 7 4 12 2

Sample Output

5

解題報告:

? ? ? ? 此題給出兩種單調棧的寫法

ac代碼1:

#include<cstdio> #include<iostream> #include<cstring> #include<stack> using namespace std; const int MAX = 80000 + 5 ; int h[MAX],r[MAX]; stack < int> sk; long long sum=0; int main() {int n;cin>>n;for(int i = 1; i<=n; i++) {scanf("%d",&h[i]);}for(int i = n; i>=1; i--) {//維護一個從右向左單調遞增減棧 //即存的是右側第一個比自己大的元素 while(!sk.empty() && h[sk.top() ] < h[i] ) sk.pop();//注意這里不能寫=!!! if(sk.empty() ) r[i]=n+1;else r[i]=sk.top();sk.push(i);}for(int i = 1; i<=n; i++) {sum+=r[i]-i-1; }printf("%lld\n",sum);return 0 ; }

?

ac代碼2:(別人寫)

#include<iostream> #include<cstdio> #include<stack> #include<algorithm> using namespace std; typedef long long LL; int main(){int n;while(~scanf("%d",&n)){stack<int>S;int t;scanf("%d",&t);S.push(t);LL ans=0;for(int i=1;i<n;i++){scanf("%d",&t);while(!S.empty()&&t>=S.top())S.pop();ans+=S.size();S.push(t);}printf("%lld\n",ans);}return 0; } /* 單調棧-----所謂單調棧也就是每次加入一個新元素時,把棧中小于等于這個值的元素彈出。 接下來回到這道題。求所有牛總共能看到多少牛,可以轉化為:這n頭牛共能被多少頭牛看見。 當我們新加入一個高度值時,如果棧中存在元素小于新加入的高度值, 那么這些小的牛肯定看不見這個高度的牛(那就看不見這頭牛后邊的所有牛), 所以就可以把這些元素彈出。每次加入新元素,并執行完彈出操作后, 棧中元素個數便是可以看見這個牛的“牛數”~~~。 */

?

總結:

? ? ?看好單調棧到底取不取等號!一個是嚴格單調一個不是嚴格單調 ?這很關鍵。

還有一種思路:代碼

我們現在只要構建一個嚴格單調遞減棧就行了對吧?假如我棧中現在有了N頭牛,按照從大到小排列,那么再加入一個高度更矮的我就再直接加入棧就好啦!但是來了一頭高個牛該怎么辦呢?,那么我們只需要把高度低于這頭牛的牛出棧就可以了不是嗎?而且嘍,我們在出棧的時候讓出棧的這頭牛的下標和現在加入的這頭牛的下標相減的絕對值再減掉一就是目前出棧的這頭牛可以看到的牛的頭數對吧?但是最后我們還需要將棧中的全部牛出棧該怎么辦呢?這時候我們只需要定義一個哨兵牛初始化為高度無限大再入棧的話前面的牛自然不就出棧了嗎?

總結

以上是生活随笔為你收集整理的【POJ - 3250 】Bad Hair Day (单调栈)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。