最少遞減子序列個數java,POJ3250 Bad Hair Day【單調遞減隊列+堆棧】

 2023-11-18 阅读 22 评论 0

摘要:Bad Hair Day Time Limit:?2000MS?Memory Limit:?65536KTotal 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

Bad Hair Day
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

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

Source

USACO 2006 November Silver

問題鏈接: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;
}




數據結構單調隊列?

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/175951.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息