✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1075 PAT Judge (pintia.cn)
🔑中文翻译:PAT 评测
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪
1075 PAT Judge
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 positive integers, N (≤104), the total number of users, K (≤5), the total number of problems, and M (≤105), the total number of submissions. It is then assumed that the user id’s are 5-digit numbers from 00001 to N, and the problem id’s are from 1 to K. The next line contains K positive integers
p[i]
(i
=1, …, K), wherep[i]
corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submission in the following format:user_id problem_id partial_score_obtained
where
partial_score_obtained
is either −1 if the submission cannot even pass the compiler, or is an integer in the range [0,p[problem_id]
]. All the numbers in a line are separated by a space.Output Specification:
For each test case, you are supposed to output the ranklist in the following format:
rank user_id total_score s[1] ... s[K]
where
rank
is calculated according to thetotal_score
, and all the users with the sametotal_score
obtain the samerank
; ands[i]
is the partial score obtained for thei
-th problem. If a user has never submitted a solution for a problem, then “-” must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id’s. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.
Sample Input:
7 4 20 20 25 25 30 00002 2 12 00007 4 17 00005 1 19 00007 2 25 00005 1 20 00002 2 2 00005 1 15 00001 1 18 00004 3 25 00002 2 25 00005 3 22 00006 4 -1 00001 2 18 00002 1 20 00004 1 15 00002 4 18 00001 3 4 00001 4 2 00005 2 -1 00004 2 0
Sample Output:
1 00002 63 20 25 - 18 2 00005 42 20 0 22 - 2 00007 42 - 25 - 17 2 00001 42 18 18 4 2 5 00004 40 15 0 25 -
题意
第一行包含三个整数 N , K , M N,K,M N,K,M,分别表示总用户数量,题目数量,以及提交数量。
用户编号是从
00001
00001
00001 到
N
N
N 的 5
位数字。
问题编号从 1 1 1 到 K K K 。
第二行包含 K K K 个整数 p 1 , p 2 , … , p K p1,p2,…,pK p1,p2,…,pK,其中 p i p_i pi 表示第 i i i 题的满分。
接下来 M M M 行,每行包含一个提交信息,包括用户编号,题目编号,以及得分。
当提交无法正确编译时,得分显示 −1
,否则是一个$ [0,该题满分]$ 范围内的整数。
注意,无法编译的情况虽然显示
−1
,但得分上算作 0 0 0 分。
以下列格式输出排名列表:
rank user_id total_score s[1] ... s[K]
其中,rank
是根据 total_score
计算的,所以拥有相同 total_score
的用户的 rank
也相同。
s[i]
是第
i
i
i 个问题获得的分数。
如果某个问题,用户从未提交过代码,则用 -
来表示这道题的分数。
如果某个问题,用户多次提交过代码,则取最高分为这道题的分数。
列表必须根据排名从前到后输出,对于排名相同的用户,根据满分题目的数量以降序对用户排序,如果仍有排名相同的情况,则按 ID
升序的顺序排序。
对于从未提交过任何代码,或者从未提交过任何编译通过的代码的用户,输出时不予考虑。
思路
- 先将每道题的满分数值用数组
p_score
保存起来。 - 输入学生的做题情况,每个学生的信息可以用结构体来存储,结构体中可以实现构造函数以及一些常用函数方便调用。最终用哈希表存储结构体,
key
为学生的id
,而value
为存储学生信息的结构体,这样就可以快速找到该学生的做题信息。 - 将满足排名条件的即要有题目提交记录并且编译通过了的学生筛选出来,用一个数组
res
存储,然后再对该数组排序,得到最终排名。 - 输出学生的排名,注意这里输出每题分数时要判断分数是否合法。
代码
#include<bits/stdc++.h>
using namespace std;
int n, k, m;
int p_score[6];
struct Student {
string id;
int score[6]; //记录每题得分
int total; //计算总分
int cnt; //记录满分题目数量
Student() {}
Student(string id_s) :id(id_s)
{
for (int i = 1; i <= 5; i++) score[i] = -2;
total = cnt = 0;
}
//计算该学生的得分情况
void cal()
{
for (int i = 1; i <= 5; i++)
{
total += max(0, score[i]);
if (score[i] == p_score[i]) cnt++;
}
}
//判断该学生是否提交过题目并且编译通过了
bool has_submit()
{
for (int i = 1; i <= k; i++)
if (score[i] >= 0)
return true;
return false;
}
bool operator < (const Student& s)const
{
if (total != s.total) return total > s.total; //按总分降序
else if (cnt != s.cnt) return cnt > s.cnt; //按满分题目降序
return id < s.id; //按学生id字典序升序
}
};
int main()
{
cin >> n >> k >> m;
//先输入题目的满分分数
for (int i = 1; i <= k; i++) cin >> p_score[i];
//输入做题情况
unordered_map<string, Student> student;
for (int i = 0; i < m; i++)
{
string id_s;
char id[10];
int index, num;
scanf("%s %d %d", id, &index, &num);
id_s = id; //将char转换成string
if (!student.count(id_s)) student[id_s] = Student(id_s);
student[id_s].score[index] = max(student[id_s].score[index], num);
}
//获取符合要求的学生信息
vector<Student> res;
for (auto& item : student)
{
auto& s = item.second;
if (s.has_submit())
{
s.cal(); //计算该学生的得分情况
res.push_back(s);
}
}
sort(res.begin(), res.end()); //对学生进行排名
//输出排名结果
for (int i = 0, rank = 1; i < res.size(); i++)
{
auto& s = res[i];
if (!i || s.total != res[i - 1].total) rank = i + 1;
printf("%d %s %d", rank, s.id.c_str(), s.total);
for (int j = 1; j <= k; j++)
if (s.score[j] == -2) printf(" -"); //没提交该题或编译没通过
else printf(" %d", max(0, s.score[j])); //最低得分为0
puts("");
}
return 0;
}