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 (≤), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤), 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

题意:

  给出每个考生的成绩,求出其在考场中的排名和总排名。

思路:

  模拟 + 排序

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 struct Node {
 6     string registration_number;
 7     int final_rank;
 8     int location_number;
 9     int local_rank;
10     int grade;
11 };
12 
13 bool cmp(Node a, Node b) {
14     if (a.grade == b.grade)
15         return a.registration_number < b.registration_number;
16     return a.grade > b.grade;
17 }
18 
19 int main() {
20     int n, k;
21     cin >> n;
22     string registration_number;
23     int grade;
24     vector<Node> testees;
25     for (int i = 1; i <= n; ++i) {
26         cin >> k;
27         vector<Node> temp(k);
28         for (int j = 0; j < k; ++j) {
29             cin >> temp[j].registration_number >> temp[j].grade;
30             temp[j].location_number = i;
31         }
32         sort(temp.begin(), temp.end(), cmp);
33         int local_rank = 1;
34         temp[0].local_rank = 1;
35         testees.push_back(temp[0]);
36         for (int j = 1; j < k; ++j) {
37             local_rank++;
38             if (temp[j].grade != temp[j - 1].grade)
39                 temp[j].local_rank = local_rank;
40             else
41                 temp[j].local_rank = temp[j - 1].local_rank;
42             testees.push_back(temp[j]);
43         }
44     }
45     sort(testees.begin(), testees.end(), cmp);
46     int final_rank = 1;
47     testees[0].final_rank = 1;
48     for (int i = 1; i < testees.size(); ++i) {
49         final_rank++;
50         if (testees[i].grade != testees[i - 1].grade)
51             testees[i].final_rank = final_rank;
52         else
53             testees[i].final_rank = testees[i - 1].final_rank;
54     }
55     cout << testees.size() << endl;
56     for (int i = 0; i < testees.size(); ++i) {
57         cout << testees[i].registration_number << " " << testees[i].final_rank
58              << " " << testees[i].location_number << " "
59              << testees[i].local_rank << endl;
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/h-hkai/p/13157647.html