A race report lists n runners in bib order, each with a performance value. For every runner, count how many of the runners listed after them (to their right in the list) have a strictly smaller value than they do.
Output these counts, one per runner, in the original bib order. The intended approach obtains all counts together while performing a single merge sort that remembers each value's original position.
Line 1: an integer n, the number of runners.
Line 2: n space-separated integers, the performance values in bib order.
A single line of n space-separated integers: for each runner (in bib order), the number of strictly-smaller values that appear later in the list.
Example 1
Input
5 5 2 6 1 3
Expected
3 1 2 0 0
Explanation
After 5 come 2, 1, 3 (three smaller); after 2 comes 1 (one); after 6 come 1, 3 (two); after 1 none; after 3 none.
Example 2
Input
4 1 2 3 4
Expected
0 0 0 0
Explanation
The values increase, so no value has a smaller value to its right; every count is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →