Altitude levels of the stations
Statement
N stations are scattered across a mountain region. The altitude above sea level has been measured at every station, written as a positive number when the station sits above sea level and as a negative number when it sits below sea level.
A researcher wants to attach a new altitude level to every station. The altitude level of a station is the number of distinct altitudes lower than that station's altitude. The lowest station therefore gets level 0, and stations at the same altitude get the same level.
For example, suppose the altitudes measured at N=5 stations are the ones in the following table.
| Station | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Altitude | 2 | 4 | -10 | 4 | -9 |
The distinct altitudes here are the four values -10, -9, 2 and 4. Laying them out from the lowest one and attaching levels from 0 onward gives the following.
| Altitude | -10 | -9 | 2 | 4 |
|---|---|---|---|---|
| Level | 0 | 1 | 2 | 3 |
Reading that table off from station 1 onward gives the following. Station 2 and station 4 sit at the same altitude, so they get the same level.
| Station | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Level | 2 | 3 | 0 | 3 | 1 |
The level of the highest station is one less than the number of distinct altitudes. The example above has four distinct altitudes, so the highest station, the one at 4 meters, gets level 3.
Now look at a case with many stations at the same altitude. Suppose the altitudes measured at N=6 stations hold only 999 meters and 1,000 meters in turn, as in the following table.
| Station | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Altitude | 1000 | 999 | 1000 | 999 | 1000 | 999 |
There are only two distinct altitudes, so only levels 0 and 1 appear. The 999-meter stations get level 0 and the 1,000-meter stations get level 1.
| Station | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Level | 1 | 0 | 1 | 0 | 1 | 0 |
There can be up to 1,000,000 stations. Going through every other station for each station to compare altitudes therefore does not finish in time.
Given the altitudes measured at the N stations, write a program that finds the altitude level of station 1 through station N in order.
Input and output examples
Example 1
Input
5 2 4 -10 4 -9
Output
2 3 0 3 1
Example 2
Input
1 849693849
Output
0
Example 3
Input
6 1000 999 1000 999 1000 999
Output
1 0 1 0 1 0
Input
The first line gives the number of stations N. N is a natural number less than or equal to 1,000,000.
The second line gives the altitudes measured at the N stations, from station 1 to station N in order, separated by one space. Each altitude is an integer from -1,000,000,000 to 1,000,000,000.
Several stations can sit at the same altitude. The altitudes are not sorted from the lowest one and come in any order.
Output
Print the altitude levels of station 1 through station N in order, on one line.
After that, print one newline and stop. Do not add a blank line.
Grading compares the output with the expected value character by character. When a line holds more than one value, the values are separated by a single space.
Requirements
- Read the input from standard input and write the answer to standard output
- The altitude level of a station is the number of distinct altitudes lower than that station's altitude. Stations at the same altitude get the same level
- Print the levels on one line, from station 1 to station N, in the order the input gives them
- An altitude can be negative
- N goes up to 1,000,000, so comparing each station against every other station does not finish in time
- Grading compares the output with the expected value down to the last character
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 849693849
0
5 2 4 -10 4 -9
2 3 0 3 1
6 1000 999 1000 999 1000 999
1 0 1 0 1 0
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.