#t548. 旗鼓相当的对手(加强版)
旗鼓相当的对手(加强版)
Well-Matched Rivals (Hard Version)
Problem Description
students took the final exam and we have each student's information: name (a string of at most characters, no spaces), and scores in Chinese, Math, and English (natural numbers not exceeding ).
A pair of students is considered "well-matched rivals" if:
- The difference in each subject's score is at most ;
- The difference in total score is at most .
Now we want to know which students are "well-matched rivals"; output their names.
All names are given in lexicographic order. The output should also list all rival pairs in lexicographic order:
- Within each pair, the first name's lexicographic order should be smaller than the second name's;
- If the first names of two pairs differ, output the pair whose first name is lexicographically smaller first;
- If the first names are the same but the second names differ, output the pair whose second name is lexicographically smaller first.
Input Format
The first line contains a positive integer , the number of students.
The following lines each start with a string (the student's name), followed by natural numbers (Chinese, Math, English scores). The string and the integers are separated by spaces.
Output Format
Output several lines, each containing space-separated strings representing one pair of well-matched rivals.
Output in the order described above.
Sample Input
3
fafa 90 90 90
lxl 95 85 90
senpai 100 80 91
Sample Output
fafa lxl
lxl senpai
Sample Explanation
There are students:
fafa's Chinese, Math, and English scores are , total ;lxl's Chinese, Math, and English scores are , total ;senpai's Chinese, Math, and English scores are , total .
For fafa and lxl:
- The three subject differences are , all ;
- The total-score difference is , . So they are well-matched rivals.
For fafa and senpai:
- The Chinese difference is , . So they are not well-matched rivals.
For lxl and senpai:
- The three subject differences are , all ;
- The total-score difference is , . So they are well-matched rivals.
Therefore, outputting these two pairs in lexicographic order gives the two lines in the sample output.
Constraints
Names have length at most ; Chinese, Math, and English scores are natural numbers not exceeding .