Identifying Well-Matched Competitors from Exam Scores

Given \(N\) students (\(N \le 1000\)), each student has a name (a string of at most 8 characters with no spaces) and three integer scores for Chinese, Mathematics, and English (each between 0 and 150). Two students are considered "well-matched competitors" if the absolute difference in each subject score is at most 5, and the absolute difference in total scores is at most 10. The task is to output all such pairs, sorted in dictionary order as specified: the first name of a pair must be lexicographically smaller than the second; pair are ordered first by the first name, then by the second name.

The input names are already in dictionary order, and the output must respect that same ordering for the pairs.

Input Format

The first line contains an integer \(N\). The next \(N\) lines each contain a student name followed by three integers representing Chinese, Mathematics, and English scores, all separated by spaces.

Output Format

Several lines, each with two names separated by a space, representing a pair of well-matched competitors. The pairs must be printed in ascending dictionary order as described.

Sample

Input:
3
fafa 90 90 90
lxl 95 85 90
senpai 100 80 91

Output:
fafa lxl
lxl senpai

Implementation Approach

We store each student's record in a structure. Then we iterate over all unique pairs \((i, j)\) with \(i < j\). For each pair, we check the condition: for each subject, \(|score_i - score_j| \le 5\) and \(|total_i - total_j| \le 10\). If satisfied, we output the two names. Since the input names are in dictionary order and we process pairs in encreasing index order, the output automatically respects the required ordering.

Code (C++)

#include <iostream>
#include <cmath>      // for std::abs
#include <string>
#include <vector>

struct Student {
    std::string name;
    int chnScore;   // Chinese
    int mathScore;  // Mathematics
    int engScore;   // English
};

bool isRival(const Student& a, const Student& b) {
    // Check each subject difference
    if (std::abs(a.chnScore - b.chnScore) > 5) return false;
    if (std::abs(a.mathScore - b.mathScore) > 5) return false;
    if (std::abs(a.engScore - b.engScore) > 5) return false;
    // Check total score difference
    int totalA = a.chnScore + a.mathScore + a.engScore;
    int totalB = b.chnScore + b.mathScore + b.engScore;
    if (std::abs(totalA - totalB) > 10) return false;
    return true;
}

int main() {
    int n;
    std::cin >> n;
    std::vector<Student> students(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> students[i].name
                   >> students[i].chnScore
                   >> students[i].mathScore
                   >> students[i].engScore;
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (isRival(students[i], students[j])) {
                // Names are already in dictionary order due to input ordering
                // and because i < j guarantees name[i] <= name[j]
                std::cout << students[i].name << " " << students[j].name << "\n";
            }
        }
    }

    return 0;
}

Tags: C++ algorithm pair comparison Sorting Problem Solving

Posted on Wed, 21 Oct 2026 16:36:42 +0000 by abselect