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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ 3250 Bad Hair Day【单调队列】

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

Description

Some of Farmer John'sNcows (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 cowihas a specified heighthi(1 ≤hi≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cowican see the tops of the heads of cows in front of her (namely cowsi+1,i+2, and so on), for as long as these cows are strictly shorter than cowi.

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!

Letcidenote the number of cows whose hairstyle is visible from cowi; please compute the sum ofc1throughcN.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: Linei+1 contains a single integer that is the height of cowi.

Output

Line 1: A single integer that is the sum ofc1throughcN.

Sample Input

6
10
3
7
4
12
2

Sample Output

5

分析:

這個題的實質是求從隊列中任意一個數開始連續(xù)下降的數的個數的和。所以需要知道從隊列中的某個數開始有多少個連續(xù)的下降的數。所以考慮使用單調隊列。由于這道題不是求一個區(qū)間的最值,所以不用記錄隊首,隊列退化為棧。而且沒有區(qū)間長度限制,所以不用記錄下標,也不用保留原數據,降低了空間復雜度。時間復雜度為O(n),注意sum的范圍。
code:

?

View Code #include<stdio.h>
int stack[80010];
int main()
{
int top=0,i,n,p;
__int64 sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&p);
while(top>0&&stack[top]<=p)
top--;
sum+=top;
stack[++top]=p;
}
printf("%I64d\n",sum);
return 0;
}


轉載于:https://www.cnblogs.com/dream-wind/archive/2012/03/14/2396291.html

總結

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

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