#K4416. Sorting Products

    ID: 27470 Type: Default 1000ms 256MiB

Sorting Products

Sorting Products

You are given a list of products, where each product is represented by a pair of integers: an identifier and an urgency score. Your task is to sort the products primarily by their urgency score in descending order. If two products have the same urgency score, they should be sorted by their identifier in ascending order.

The sorting should be implemented using standard input and output. Pay attention to efficiency and stability in your algorithm.

Sorting Criterion:

  • Sort by urgency score: higher scores come first (i.e., descending order).
  • For equal urgency scores, sort by identifier in ascending order.

Mathematically, if each product is represented as a tuple \((id, score)\), you are required to sort so that for two products \(a=(id_a, score_a)\) and \(b=(id_b, score_b)\), product \(a\) comes before product \(b\) if and only if

\[ (score_a > score_b) \quad \text{or} \quad (score_a = score_b \text{ and } id_a < id_b) \]

inputFormat

The first line of input contains a single integer T, representing the number of products. Each of the following T lines contains two space-separated integers: the product identifier and its urgency score.

outputFormat

Output T lines. Each line should contain two space-separated integers representing the product identifier and its urgency score, in the sorted order described above.## sample

5
101 5
305 3
205 5
402 2
104 5
101 5

104 5 205 5 305 3 402 2

</p>