#K90717. Sort Books by Popularity

    ID: 37815 Type: Default 1000ms 256MiB

Sort Books by Popularity

Sort Books by Popularity

Heidi has a collection of n books, each with a given popularity score. Your task is to reorder the books such that the most popular ones come first. In the case where two books have the same popularity, the book with the smaller index should come first.

The problem can be formalized as follows:

Given an integer \(n\) and a list \(popularity[1 \ldots n]\) where \(popularity[i]\) represents the popularity score of the \(i^{th}\) book, compute an ordering of the book indices such that the following conditions are met:

  • Books with higher popularity appear before those with lower popularity.
  • If two books have the same popularity, the book with the smaller index comes first.

Example: For \(n = 5\) and \(popularity = [4, 3, 2, 5, 1]\), the sorted indices should be \(4, 1, 2, 3, 5\), because the 4th book has the highest popularity, while the remaining are sorted accordingly.

inputFormat

The input is given from standard input in the following format:

 n
 a1 a2 ... an

Where:

  • n (\(1 \leq n \leq 10^5\)): The number of books.
  • a1, a2, ..., an (each \(a_i\) is an integer representing the popularity of the \(i^{th}\) book).

outputFormat

Output to standard output a single line containing the indices of the books sorted according to the rules described. The indices should be separated by a single space.

## sample
5
4 3 2 5 1
4 1 2 3 5

</p>