#K48847. Competition Ranking System
Competition Ranking System
Competition Ranking System
You are given a series of queries to simulate a competitive ranking system. There are three types of queries:
- ADD x y: Add y points to the participant with id x. If this participant is not already registered, register them and note the order in which they were added.
- REMOVE x y: Remove y points from the participant with id x. If after removal the participant's points are not positive, remove the participant from the system.
- RANKS: Output the ranking of all participants currently in the system.
The ranking is determined by the following rules:
- A participant with a higher score is ranked higher.
- In case of a tie (same score), the participant who was registered earlier (i.e. whose order is lower) is ranked higher.
This can be expressed mathematically as follows:
$$\text{ranking} = sort\Big(\{(id, score)\}\Big,\;\text{key} = \big(-score, order\big)\Big)$$Your task is to process a sequence of queries from standard input and output the rankings for each RANKS
query.
inputFormat
The first line of input contains an integer Q
, the number of queries. Each of the next Q
lines contains one query. A query is one of the following formats:
ADD <participant> <score>
REMOVE <participant> <score>
RANKS
Input is read from standard input (stdin).
outputFormat
For each RANKS
query, output one line containing the ranking of participants in the following format:
A list of tuples, where each tuple is formatted as (participant, score)
. The tuples should be enclosed in square brackets and separated by commas and a space.
For example: [(3, 200), (2, 150), (1, 50)]
Output is written to standard output (stdout).
## sample10
ADD 1 100
ADD 2 150
ADD 3 200
REMOVE 1 50
RANKS
ADD 2 100
RANKS
ADD 1 150
REMOVE 3 100
RANKS
[(3, 200), (2, 150), (1, 50)]
[(2, 250), (3, 200), (1, 50)]
[(2, 250), (1, 200), (3, 100)]
</p>