#C4323. Organize Books

    ID: 47849 Type: Default 1000ms 256MiB

Organize Books

Organize Books

You are given a list of books where each book is represented as a tuple containing the genre, title, and page count. Your task is to organize these books by applying the following ordering:

  • First, sort by genre in lexicographical order.
  • Within the same genre, sort by page count in ascending order.
  • If the page count is the same, sort by title in lexicographical order.

Implement a solution that reads the book information from standard input and outputs the sorted list to standard output.

Note: Assume that all genres and titles are single words without spaces.

inputFormat

The first line contains an integer \( n \), representing the number of books. Each of the following \( n \) lines contains a string for the genre, a string for the title, and an integer for the page count, separated by spaces.

For example:

5
Fantasy HarryPotter 500
Science Cosmos 300
Fantasy LOTR 1000
Science BriefHistory 300
Fantasy Eragon 700

outputFormat

Output the organized list of books. Each book should be printed on a new line in the following format:

genre title page_count

For the sample input above, the expected output is:

Fantasy HarryPotter 500
Fantasy Eragon 700
Fantasy LOTR 1000
Science BriefHistory 300
Science Cosmos 300
## sample
5
Fantasy HarryPotter 500
Science Cosmos 300
Fantasy LOTR 1000
Science BriefHistory 300
Fantasy Eragon 700
Fantasy HarryPotter 500

Fantasy Eragon 700 Fantasy LOTR 1000 Science BriefHistory 300 Science Cosmos 300

</p>