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.
- Assistant
To solve this problem efficiently, we need to compute, for each building at position i, the number of buildings j > i such that there is no building taller than Building j between Buildings i and j.
We will use an O(N) stack-based approach to compute the values of c[i], where c[i] represents the number of positions j > i satisfying the given condition for building i.
Here's the algorithm:
-
Initialize an array
cof length N (number of buildings) to store our results. Setc[N] = 0, since there are no buildings after the last one. -
Iterate from the second last building to the first one (i from N-1 down to 1).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.