P5741 [Shenji 7.Example 10] Evenly Matched Opponents - Enhanced Version.

Description

There are $N(N\le 1000)$ students taking the final exam, and we are given each student's information: name (a string of no more than $8$ characters, with no spaces), and scores in Chinese, Math, and English (all natural numbers not exceeding $150$). If for a pair of students $\text{}$, the score difference in each subject is no more than $5$, and the total score difference is no more than $10$, then this pair of students are "evenly matched opponents". Now we want to know, among these students, which pairs are "evenly matched opponents". Please output their names. All names are given in lexicographical order, and when outputting, all opponent pairs should also be printed in lexicographical order. That is, for each pair, the lexicographical order of the first name should be smaller than the second. If the first names of two pairs are different, output the pair with the smaller first name first. If two pairs have the same first name but different second names, output the pair with the smaller second name first.

Input Format

The first line contains a positive integer $N$, representing the number of students. Starting from the second line, the next $N$ lines follow. In each line, first input a string representing the student's name, then input three natural numbers representing the scores in Chinese, Math, and English. They are separated by spaces.

Output Format

Output several lines. Each line contains two strings separated by a space, representing a pair of evenly matched opponents. Note the output ordering described in the problem statement.

Explanation/Hint

Constraints: $1 \leq N \leq 1000$. Names are strings of length at most $8$. Scores in Chinese, Math, and English are all natural numbers not exceeding $150$. Translated by ChatGPT 5