【PAT甲级 - C++题解】1025 PAT Ranking

简介: 【PAT甲级 - C++题解】1025 PAT Ranking

1025 PAT Ranking


Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:


Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:


For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

题意

给定不同区域的学生的考号和成绩,让我们将所有成绩合并在一起,然后按成绩从高到低输出,如果成绩相同就按考号的升序输出。输出格式如下:

registration_number final_rank location_number local_rank


也就是输出考号,最终排名,地区编号,地区排名。

思路


先处理各区域的排名,学生的所有信息用结构体存起来,每个区域都用一个临时数组 temp 存起来,输入完后对 temp 进行排序,然后再去更新该区域学生的区域排名信息并放到总容器 all 中。

处理完各区域再处理总排名,所有学生信息都在总容器 all 中,对其进行排序,然后再更新每个学生的总排名信息。

最后按照格式输出总容器中的学生信息。

代码

#include<bits/stdc++.h>
using namespace std;
struct Student {
    string id;
    int grade;
    int location_number, final_rank, local_rank;
    bool operator < (const Student& s)const
    {
        if (grade != s.grade)    return grade > s.grade;
        return id < s.id;
    }
};
vector<Student> all;
int main()
{
    int n, k;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> k;
        vector<Student> temp;
        //输入基本信息
        for (int j = 1; j <= k; j++)
        {
            string id;
            int grade;
            cin >> id >> grade;
            temp.push_back({ id,grade,i });
        }
        sort(temp.begin(), temp.end());  //进行区域排名
        for (int j = 0; j < k; j++)    //更新区域排名信息
        {
            if (!j || temp[j].grade != temp[j - 1].grade)  //和前一个分数相同
                temp[j].local_rank = j + 1;
            else    //和前一个分数不同
                temp[j].local_rank = temp[j - 1].local_rank;
            all.push_back(temp[j]);
        }
    }
    sort(all.begin(), all.end());    //进行总排名
    for (int i = 0; i < all.size(); i++)   //更新总排名信息
    {
        if (!i || all[i].grade != all[i - 1].grade)  //和前一个分数相同
            all[i].final_rank = i + 1;
        else    //和前一个分数不同
            all[i].final_rank = all[i - 1].final_rank;
    }
    //输入总排名
    cout << all.size() << endl;
    for (auto& s : all)
    {
        cout << s.id << " " << s.final_rank << " " << s.location_number
            << " " << s.local_rank << endl;
    }
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
64 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
81 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
75 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
73 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
76 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
76 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
128 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
55 0
|
14天前
|
存储 编译器 对象存储
【C++打怪之路Lv5】-- 类和对象(下)
【C++打怪之路Lv5】-- 类和对象(下)
19 4
|
14天前
|
编译器 C语言 C++
【C++打怪之路Lv4】-- 类和对象(中)
【C++打怪之路Lv4】-- 类和对象(中)
16 4