Time Limit:?2000MS | ? | Memory Limit:?65536K |
Total Submissions:?20834 | ? | Accepted:?7120 |
Description
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.
最少遞減子序列個數java。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!
隊列poll函數、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
Output
Sample Input
6 10 3 7 4 12 2
Sample Output
5
Source
問題鏈接:POJ3250 Bad Hair Day
單調遞減區間?問題簡述:(略)
問題分析:
? 一群高度不同的牛從左到右排開,每頭牛只能看見它右邊的比它矮的牛的發型,若遇到一頭高度大于或等于它的牛,則無法繼續看到這頭牛后面的其他牛。求所有的牛能夠看見發型總數。
有關單調區間的題列表格,? 這是一個單調遞減隊列問題,即計算連續遞減長度問題。該問題算的是遞減和,不是區間值,所以只需要一個堆棧就可以了。高度遞減的時候就進棧,來個高個子則堆棧中的低個子彈出即可。
? 用一個數組和一個指針就可以模擬一個堆棧。? ?
程序說明:(略)
單調隊列和優先隊列、題記:(略)
參考鏈接:(略)
單調隊列好題,AC的C++語言程序如下:
/* POJ3250 Bad Hair Day */#include <iostream>
#include <stdio.h>using namespace std;int const N = 80000;
int stack[N], top;int main()
{int n, a;long long ans = 0;scanf("%d", &n);for(int i=1; i<=n; i++) {scanf("%d", &a);while (top > 0 && stack[top - 1] <= a)top--;ans += top;stack[top++] = a;}printf("%lld\n", ans);return 0;
}
數據結構單調隊列?
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态