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), where p[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 the total_score
, and all the users with the same total_score
obtain the same rank
; and s[i]
is the partial score obtained for the i
-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 -
思路
【前排提示】:这题的坑点略多。
题目大意就是给你一个类似PTA里实时提交的列表,然后让你统计每个用户的最高得分(划重点:最高得分)、总分以及排名。
一开始我的想法很简单,就是还是把读入的数据每个放入vector里面,但是后来一想,一个id代表着一个用户,读入的数据可能出现多个重复的id,那么如果放入vector里面排序的话,不就成了对一个用户的多次提交结果进行排序了吗,显然,这样的想法是不可取的。
于是呢,我就选择了用结构体数组来写,下标正好作为用户的id,这样的话正好一个下标对应一个用户的所有信息。统计完之后按照题目的要求进行排序(三个标尺)并且再计算出每个用户的rank等级即可。排序的过程就不多说了,是非常基础的,这里提一下这题的坑点:
- 要注意“-”是何时输出:【If a user has never submitted a solution for a problem, then “-” must be printed at the corresponding position】,也就是说,只有一个用户从未提交过某一题,那一题对应的位置才输出“-”,就算提交了是编译没通过的结果,那也要输出“0”,而不是“-”,我就是这个地方没注意到,一直只AC了两个测试点……所以说读题一定要仔细呀……
- 另外,就算某个人的总分是0分,也是要输出的,所以需要另设一个flag来判断这个人的信息是否应该被输出;
- 因为要计算的是最高得分,如果某个人之前在某一题已经得到了一定的分数,但是突然提交了一次编译未通过,得到的分数是-1,这个时候就要判断,它之前的这个题目的分数是不是-1,如果是-1,说明这题是第一次提交,需要改成0,如果不是-1的话,就不要改啦,否则0分就会覆盖掉之前的分数~
代码
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 100010;
struct student{
bool flag;
int user_id, total_score, Rank, perfectly_solved;
int s[6];
student(){
flag = false;
user_id = total_score = Rank = perfectly_solved = 0;
memset(s, -1, sizeof(s));
}
}user[maxn];
int p[6] = {0};
bool cmp(student a, student b){
if(a.total_score!=b.total_score) return a.total_score>b.total_score;
else if(a.perfectly_solved!=b.perfectly_solved) return a.perfectly_solved>b.perfectly_solved;
else return a.user_id<b.user_id;
}
int main()
{
int N, K, M;
scanf("%d%d%d", &N, &K, &M);
for(int i=1;i<=K;i++){
scanf("%d", &p[i]);
}
for(int i=0;i<M;i++){
int tmpid, problem_id, partial_score_obtained;
scanf("%d %d %d", &tmpid, &problem_id, &partial_score_obtained);
user[tmpid].user_id = tmpid;
if(partial_score_obtained==-1){
if(user[tmpid].s[problem_id]==-1) user[tmpid].s[problem_id] = 0;
continue;
}
else{
user[tmpid].flag = true;
if(user[tmpid].s[problem_id]<partial_score_obtained){
user[tmpid].s[problem_id] = partial_score_obtained;
if(user[tmpid].s[problem_id]==p[problem_id]) user[tmpid].perfectly_solved++;
}
}
}
for(int i=1;i<=N;i++){
for(int j=1;j<=K;j++){
if(user[i].s[j]==-1) continue;
user[i].total_score += user[i].s[j];
}
}
sort(user+1, user+N+1, cmp);
int LastRank = 0, LastScore = 0;
for(int i=1;i<=N;i++){
if(i==1){
user[i].Rank = i;
LastRank = user[i].Rank;
LastScore = user[i].total_score;
}
else{
if(user[i].total_score==LastScore){
user[i].Rank = LastRank;
}
else{
user[i].Rank = i;
LastRank = user[i].Rank;
LastScore = user[i].total_score;
}
}
}
for(int i=1;i<=N;i++){
if(user[i].flag==false) continue;
printf("%d %05d %d", user[i].Rank, user[i].user_id, user[i].total_score);
for(int j=1;j<=K;j++){
if(user[i].s[j]==-1) printf(" -");
else printf(" %d", user[i].s[j]);
}
printf("\n");
}
return 0;
}