#C6029. Top K Books Selection
Top K Books Selection
Top K Books Selection
You are given a list of books and a list of queries. Each book is described by its title, price, and rating. For each query, you will be provided with a criterion ('price' or 'rating') and an integer \(k\). Your task is to output the top \(k\) books according to the specified criterion.
For a query with the criterion price, you should sort the books in ascending order by price. If two books have the same price, sort them in lexicographical order by title.
For a query with the criterion rating, you should sort the books in descending order by rating. If two books have the same rating, sort them in lexicographical order by title.
You will be given multiple queries. For each query, print a single line containing the titles of the top \(k\) books (separated by a space).
inputFormat
The input is read from standard input and has the following format:
- An integer \(n\) representing the number of books.
- \(n\) lines follow, each containing a book's title (string without spaces), price (integer), and rating (integer), separated by spaces.
- An integer \(q\) representing the number of queries.
- \(q\) lines follow, each containing a criterion (either 'price' or 'rating') and an integer \(k\), separated by a space.
outputFormat
For each query, output a single line containing the titles of the top \(k\) books separated by a single space. The order should follow the sorting rule based on the query's criterion.
## sample5
HarryPotter 300 95
LordOfTheRings 500 97
ToKillAMockingbird 400 80
TheGreatGatsby 150 85
WarAndPeace 450 99
1
price 3
TheGreatGatsby HarryPotter ToKillAMockingbird
</p>