Problem Statement There are N buildings, Building 1, Building 2, …, Building N, arranged…
- User
Problem Statement There are N buildings, Building 1, Building 2, …, Building N, arranged in a line in this order. The height of Building i (1≤i≤N) is H i .
For each i=1,2,…,N, find the number of integers j (i<j≤N) satisfying the following condition:
There is no building taller than Building j between Buildings i and j. Constraints 1≤N≤2×10 5
1≤H i ≤N H i
=H j (i =j) All input values are integers. Input The input is given from Standard Input in the following format:
N H 1
H 2
… H N
Output For each i=1,2,…,N, let c i be the number of j satisfying the condition. Print c 1 ,c 2 ,…,c N in order, separated by spaces.
Sample Input 1 Copy 5 2 1 4 3 5 Sample Output 1 Copy 3 2 2 1 0 For i=1, the integers j satisfying the condition are 2, 3, and 5: there are three. (Between Buildings 1 and 4, there is a building taller than Building 4, which is Building 3, so j=4 does not satisfy the condition.) Therefore, the first number in the output is 3.
Sample Input 2 Copy 4 1 2 3 4 Sample Output 2 Copy 3 2 1 0 Sample Input 3 Copy 10 1 9 6 5 2 7 10 4 8 3 Sample Output 3 Copy 2 3 3 3 2 1 2 1 1 0
- Assistant
This problem involves determining, for each building, how many buildings to its right satisfy a certain condition based on heights. Specifically, for Building ( i ), we need to count how many buildings ( j > i ) have no taller building than ( H_j ) between ( i ) and ( j ).
A stack-based solution can solve this problem efficiently in ( O(N) ) time. Here is an outline of the approach:
-
: Start with an empty stack that will store the indices of the buildings.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.