Determining the Winning Team in a Programming Contest

In a programming team competition, each team consists of multiple members who compete individually. The team's total score is the sum of all its members' scores, and the team with the highest total wins. Given the scores of all participants, write a program to identify the champion team.

Input Format

The first line provides a positive integer N (≤10⁴), representing the total number of participants. The following N lines each contain a participant's score in the format: teamID-memberID score, where teamID is an integer from 1 to 1000, memberID is an integer from 1 to 10, and score is an integer from 0 to 100.

Output Format

Output the champion team's ID and total score on a single line, separated by a space. The problem guarantees a unique winner.

Sample Input

6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61

Sample Output

11 176

Implementation

A straightforward approach uses an array to accumulate scores by team ID. Initialize an array of size 1001 (indexed 1–1000) to zero. For each input line, parse the team ID and score, ignoring the member ID, and add the score to the corresponding array element. After processing all inputs, iterate through the array to find the maximum total and its associated team ID.

#include <stdio.h>
int main() {
    int teamScores[1001] = {0};
    int N, team, score;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        scanf("%d-%*d %d", &team, &score);
        teamScores[team] += score;
    }
    int maxScore = 0, winner = 0;
    for (int j = 1; j <= 1000; j++) {
        if (teamScores[j] > maxScore) {
            maxScore = teamScores[j];
            winner = j;
        }
    }
    printf("%d %d", winner, maxScore);
    return 0;
}

This method uses two loops: one for input processing and another for finding the maximum. Its efficient with O(N) time complexity for input and O(1000) for the search, suitable for the given constraints.

An alternative approach using a dynamic structure, such as an array of teams with tracking via pointers, involves more loops and conditional checks. While functional, it is less efficient for large datasets due to increased overhead in searching for existing teams during input processing.

#include <stdio.h>
typedef struct {
    int teamID;
    int totalScore;
} Team;
int main() {
    Team teams[1000];
    Team *current = teams;
    int N, team, score;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        scanf("%d-%*d %d", &team, &score);
        Team *ptr = teams;
        while (ptr != current) {
            if (ptr->teamID == team) {
                ptr->totalScore += score;
                goto skip;
            }
            ptr++;
        }
        current->teamID = team;
        current->totalScore = score;
        current++;
        skip:;
    }
    int maxScore = 0, winnerID = 0;
    Team *iter = teams;
    while (iter != current) {
        if (iter->totalScore > maxScore) {
            maxScore = iter->totalScore;
            winnerID = iter->teamID;
        }
        iter++;
    }
    printf("%d %d", winnerID, maxScore);
    return 0;
}

This version uses three loops: one for input with an inner search loop and another for finding the maximum. It can become slower with many team due to linear searches, making the array-based method preferable for performance and simplicity.

Tags: programming algorithm C competition Data Structures

Posted on Sun, 26 Jul 2026 16:25:41 +0000 by egiblock