单词积累
- simultaneously 同时地
- immediately 立即 立刻
- registration 登记 注册
题目
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
结尾无空行
思路
-
分考场排序,再整合排序。典型的结构体排序,利用stl里面的sort函数即可。需要注意两点:
- 分数相同时,按id号排序,涉及字符串比较函数(算法笔记做法)
函数原型:int mystrcmp(const char *src,const char *dst)
小于返回值:-1
等于返回值:0
大于返回值:1
- id号处理为字符串更合适,因为若编号为0000003,会被数字变量处理为3;或者采用格式化输出
printf("%013lld",a);
-
结构体用什么数据结构存储呢?
- 向量的话,不方便做部分长度的数据排序,柳神的做法是使用多个向量,每个向量对应每个考场,最后再合到大向量里,这样的话;
- 如果使用数组的话,在标号上要格外小心。
代码
#include <bits/stdc++.h>
using namespace std;
struct testee {
char id[14];
int score;
int final_rank;
int location_number;
int local_rank;
}tester[30010];
bool bmp (testee a, testee b) {
if (a.score == b.score) {
return strcmp(a.id, b.id) < 0;
}
return a.score > b.score;
}
int main() {
int N;
scanf("%d", &N);
int flag = 1;
testee fir;
fir.score = 200;
tester[0] = fir;
for (int i = 1; i <= N; i++) {
int K;
scanf("%d", &K);
testee now;
for (int j = 0; j < K; j++) {
scanf("%s%d", now.id, &now.score);
now.location_number = i;
tester[flag+j] = now;
}
sort(tester + flag, tester + flag + K, bmp);
for (int j = flag; j < flag + K; j++) {
if (tester[j].score == tester[j-1].score) {
tester[j].local_rank = tester[j-1].local_rank;
} else {
tester[j].local_rank = j - flag + 1;
}
}
flag += K;
}
sort(tester + 1, tester + flag, bmp);
cout<<flag-1;
for (int i = 1; i < flag; i++) {
if (tester[i].score == tester[i-1].score) {
tester[i].final_rank = tester[i-1].final_rank;
} else {
tester[i].final_rank = i;
}
cout<<endl;
cout<<tester[i].id<<" "<<tester[i].final_rank<<" "<<tester[i].location_number<<" "<<tester[i].local_rank;
}
}