原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677
题目如下:
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, NNN (≤104le 10^4≤104), the total number of users, KKK (≤5le 5≤5), the total number of problems, and MMM (≤105le 10^5≤105), the total number of submissions. It is then assumed that the user id's are 5-digit numbers from 00001 to NNN, and the problem id's are from 1 to KKK. The next line contains KKK positive integers p[i]
(i
=1, ..., KKK), where p[i]
corresponds to the full mark of the i-th problem. Then MMM 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-1−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 -
__________________________________________________________________________________________________________________________
这道题首先要对题意理解清楚,就是对输入的用户数据进行排序,分数高的先输出,如果分数相等,则比较完全正确的解答的数量,如果再相等,则按照序号升序输出;
另外输出的时候,要注意是编译未通过(即输入-1)和为提交任何题目的(需要做标记)不用输出。
题目的核心在于排序,我是利用的c语言中的qsort.另外,对题目的逻辑分析和代码实现也是一个难点;最后,初始的用户序号要为最大,否则排序会有问题(在代码中
已经注释)导致最后一个测试会通不过。
解答该题的时候也参考了其他博主的代码!
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define K 6 4 #define N 10001 5 6 typedef struct user{ 7 int uuid; 8 int rank; 9 int total_score; 10 int p[K]; 11 int perfectly_score; 12 int pass; 13 }User; 14 15 User uu[N]; 16 17 int cmp(const void *u1,const void *u2) 18 { 19 User *uu1=(User *)u1; 20 User *uu2=(User *)u2; 21 if (uu2->total_score>uu1->total_score)return 1; 22 else if (uu2->total_score==uu1->total_score) 23 { 24 if (uu2->perfectly_score>uu1->perfectly_score)return 1; 25 else if (uu2->perfectly_score==uu1->perfectly_score)return (uu1->uuid-uu2->uuid); 26 } 27 return -1; 28 } 29 30 int main() 31 { 32 int n,k,m,score[K]={0}; 33 int i,j,uuid,pro,sco; 34 scanf("%d %d %d",&n,&k,&m); 35 for (i=1;i<=k;i++)scanf("%d",&score[i]); 36 /* 初始化 */ 37 for (i=0;i<n;i++) 38 { 39 uu[i].perfectly_score=0; 40 uu[i].total_score=0; 41 uu[i].pass=0; 42 uu[i].uuid=N; //如果id不初始为最大,那么没有通过编译的会排在通过编译但得分为0的同学之前, 43 // 从而影响得分为0的同学的rank值 44 for (j=1;j<=k;j++) 45 { 46 uu[i].p[j]=-2; 47 } 48 } 49 /* 将每个用户解决的相应问题的相应最大得分进行纪录 */ 50 for (i=0;i<m;i++) 51 { 52 scanf("%d %d %d",&uuid ,&pro,&sco); 53 if (uu[uuid-1].p[pro]<sco)uu[uuid-1].p[pro]=sco; 54 } 55 /* 将数据进行整理 */ 56 for (i=0;i<n;i++) 57 { 58 int flag,total,perfect; 59 flag=total=perfect=0; 60 for (j=1;j<=k;j++) 61 { 62 if (uu[i].p[j]>=0) //>=0 说明该用户有通过了编译的 63 { 64 flag=1; 65 total+=uu[i].p[j]; 66 if (uu[i].p[j]==score[j])perfect++; 67 } 68 } 69 if (flag) 70 { 71 uu[i].uuid=i+1; 72 uu[i].perfectly_score=perfect; 73 uu[i].total_score=total; 74 uu[i].pass=1; 75 } 76 } 77 78 qsort(uu,n,sizeof(uu[0]),cmp); 79 80 uu[0].rank=1; 81 printf("%d %05d %d",uu[0].rank,uu[0].uuid,uu[0].total_score); 82 for (j=1;j<=k;j++) 83 { 84 if (uu[0].p[j]>=0)printf(" %d",uu[0].p[j]); 85 else if (uu[0].p[j]==-1)printf(" 0"); //单独处理-1,即没有通过编译得分为0 86 else printf(" -"); // 没有提交输出 - 87 } 88 printf(" "); 89 90 for (i=1;i<n;i++) 91 { 92 if (uu[i].pass>0 ){ 93 if (uu[i].total_score==uu[i-1].total_score)uu[i].rank=uu[i-1].rank; 94 else uu[i].rank=i+1; // 注意是i+1 95 printf("%d %05d %d",uu[i].rank,uu[i].uuid,uu[i].total_score); 96 for (j=1;j<=k;j++) 97 { 98 if (uu[i].p[j]>=0)printf(" %d",uu[i].p[j]); 99 else if (uu[i].p[j]==-1) printf(" 0"); 100 else printf(" -"); 101 } 102 printf(" ");} 103 } 104 105 return 0; 106 }