1 条题解

  • 0
    @ 2025-4-10 12:37:08

    问题分析

    本题要求根据编程竞赛的规则生成参赛队伍的排名列表。需要处理多组测试用例,每组测试用例包含队伍信息、问题数量、提交记录,需要根据规则计算每个队伍的正确解题数量和总用时,再根据这些信息进行排名。规则如下:

    队伍按正确解题数量排名,解题数量多的队伍排名靠前。解题数量相同的队伍,按总用时少的排名靠前。 若解题数量和总用时都相同,则排名相同,按队伍名称字母顺序排列。已解决问题的总用时为首次正确提交时间加上该问题之前错误提交的罚时(每次错误提交罚 2020 分钟),正确提交后再提交错误不罚时。

    解题思路

    数据结构设计:设计一个 Team 结构体来存储队伍的信息,包括队伍名称、解题数量、总用时。 读取输入:读取测试用例数量,对于每个测试用例,读取队伍数量、队伍名称、问题数量和提交记录。 处理提交记录:对于每条提交记录,更新对应队伍的解题信息和总用时。

    排序:根据解题数量和总用时对队伍进行排序,若解题数量和总用时相同,则按队伍名称字母顺序排序。 计算排名:遍历排序后的队伍列表,根据规则计算每个队伍的排名。

    输出结果:按照指定格式输出每个队伍的排名、名称、解题数量和总用时。

    cpp

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <string>
      #include <iomanip>
    

    // 定义队伍结构体 struct Team { std::string name; int solved; int time; Team(const std::string& n) : name(n), solved(0), time(0) {} };

    // 自定义比较函数,用于排序队伍 bool compareTeams(const Team& a, const Team& b) { if (a.solved != b.solved) { return a.solved > b.solved; } if (a.time != b.time) { return a.time < b.time; } return a.name < b.name; }

    // 处理每个测试用例 void processScenario() { int n; std::cin >> n; std::vector teams; for (int i = 0; i < n; ++i) { std::string teamName; std::cin >> teamName; teams.emplace_back(teamName); }

    int k, m;
    std::cin >> k >> m;
    
    // 记录每个队伍每个问题的错误提交次数
    std::vector<std::vector<int>> wrongSubmissions(n, std::vector<int>(k, 0));
    // 记录每个队伍每个问题是否已解决
    std::vector<std::vector<bool>> solved(n, std::vector<bool>(k, false));
    
    for (int i = 0; i < m; ++i) {
        int problem, time;
        std::string correctness, teamName;
        std::cin >> problem >> time >> correctness >> teamName;
    
        // 找到队伍的索引
        int teamIndex = -1;
        for (int j = 0; j < n; ++j) {
            if (teams[j].name == teamName) {
                teamIndex = j;
                break;
            }
        }
    
        if (correctness == "Yes" && !solved[teamIndex][problem - 1]) {
            teams[teamIndex].solved++;
            teams[teamIndex].time += time + wrongSubmissions[teamIndex][problem - 1] * 20;
            solved[teamIndex][problem - 1] = true;
        } else if (correctness == "No" && !solved[teamIndex][problem - 1]) {
            wrongSubmissions[teamIndex][problem - 1]++;
        }
    }
    
    // 对队伍进行排序
    std::sort(teams.begin(), teams.end(), compareTeams);
    
    // 计算排名
    int rank = 1;
    for (int i = 0; i < n; ++i) {
        if (i > 0 && (teams[i].solved != teams[i - 1].solved || teams[i].time != teams[i - 1].time)) {
            rank = i + 1;
        }
        std::cout << std::setw(2) << rank << ". " << std::left << std::setw(8) << teams[i].name
                  << std::right << std::setw(1) << teams[i].solved
                  << std::right << std::setw(4) << teams[i].time << std::endl;
    }
    std::cout << std::endl;
    

    }

    int main() { int scenarios; std::cin >> scenarios; for (int i = 0; i < scenarios; ++i) { processScenario(); } return 0; }

    • 1

    信息

    ID
    919
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    0
    上传者