题目描述
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.
输入格式:
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.
输出格式:
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.
输入样例:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
输出样例:
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
题目大意:
输入N个考场,每个考场包含K个学生,每个学生有编号和成绩两部分信息,对每个考场的考生和所有考生按成绩进行排名,如果有两个考生成绩相同,则这两个考生排名一致。最后输出总人数,每个考生编号,总排名,考场号和考场内排名。
注意:若两个考生排名一致,则应按编号从小到大输出。
分析:
利用sort()函数先对单个考场内人按成绩从大到小进行排序,并得到考场内排名,若成绩相同,则按编号从小到大排序,且排名相同。最后再对所有人以同样的方法进行排序,并得到总排名。
#include<iostream>
#include <algorithm>
using namespace std;
struct testee {
string number;//编号
int score;//得分
int location;//考场
int local_rank;//考场内排名
int final_rank;//总排名
} stu[30001];
bool cmp(testee a, testee b) {
if (a.score != b.score)
return a.score > b.score;
else
return a.number < b.number;
}
int main() {
int N;//考场
cin >> N;
int count = 0;//记录总人数
int start = count;//记录每个考场人数的起始位置
for (int i = 1; i <= N; ++i) {
int K;//每个考场人数
cin >> K;
for (int j = 0; j < K; ++j) {//各个考生信息
string num;
int score;
cin >> num >> score;
stu[count].number = num;
stu[count].score = score;
stu[count].location = i;
count++;
}
sort(stu + start, stu + count, cmp);
stu[start].local_rank = 1;
for (int k = 1; k < K; ++k) {
if (stu[k + start].score != stu[k + start - 1].score)
stu[k + start].local_rank = k + 1;
else {
stu[k + start].local_rank = stu[k + start - 1].local_rank;
}
}
start = count;
}
sort(stu, stu + count, cmp);
stu[0].final_rank = 1;
for (int l = 1; l < count; ++l) {
if (stu[l].score != stu[l - 1].score)
stu[l].final_rank = l + 1;
else {
stu[l].final_rank = stu[l - 1].final_rank;
}
}
cout << count << endl;
for (int m = 0; m < count; ++m) {
cout << stu[m].number << " " << stu[m].final_rank << " " << stu[m].location << " " << stu[m].local_rank
<< endl;
}
return 0;
}