中国大学MOOC-陈越、何钦铭-数据结构
10-排序5 PAT Judge (25 分)
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 -
算法思想:
这道题做得挺心酸的,因为单看题目就看了仅半个钟(还看错了= =!)题目解析懒得写了,大家参考另一个博主的贴:https://blog.csdn.net/lunaticzhg/article/details/80330491 写得贼详细。
主要考察的是按主次关键字多重排序,我用LSD,排序权衡从主到次为:总分,完美解决数量,id序号。
有个小坑就是完美提交次数同一道题重复提交不累计。
测试点2:提交通过实得0分和提交不通过定义为0分的区分。
测试点4:检查排序算法,是否稳定,>,<,==三者关系是否正确。
/***************2019.5.16-09:28-12:00 12:30-13:19**************/
/***************2019.5.17-08:15-09:24**************/
//10-排序5 PAT Judge 3.5h
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct UserNode *UserPtr;
struct UserNode{
int Order;
int PS[6];//Partital_score_obtained
//-2:mean never submitted ;-1:mean submitted but can't pass
int TS;//total score
int Pe_N;//the number of perfectly solved problems
};//User[10008] --用malloc
//注意cmp函数的用法,return 1表示不换位置,return 0表示互换位置
UserPtr User;
bool cmp(int a,int b){
if(User[a].TS>User[b].TS) return 1;
else if (User[a].TS<User[b].TS) return 0;
else {
if(User[a].Pe_N>User[b].Pe_N) return 1;
else if (User[a].Pe_N<User[b].Pe_N) return 0;
else {
if(a>b) return 0;
else return 1;
}
}
}
int main(){
int i,j,k;
int N,K,M;
scanf("%d%d%d",&N,&K,&M);
int p[K+1];//corresponds to the full mark of the i-th problem
for(i=1;i<=K;i++) scanf("%d",&p[i]);
User=(UserPtr)malloc((N+1)*sizeof(struct UserNode));
int User_Sort[N+1];//表排序
for(i=1;i<=N;i++){ //初始化用户结点与表排序数组
User_Sort[i]=i;
User[i].Pe_N=0;
User[i].TS=0;
for(j=1;j<=K;j++) User[i].PS[j]=-2;
}
int Uid,Pid,score;//User_id,Problem_id,partital_score_obtained;
for(i=0;i<M;i++){
scanf("%d%d%d",&Uid,&Pid,&score);
if(User[Uid].PS[Pid]<score){
User[Uid].PS[Pid]=score;
}
}
int flag;
for(i=1;i<=N;i++){
flag=1;
for(j=1;j<=K;j++){
if(User[i].PS[j]>=0){
flag=0;
User[i].TS+=User[i].PS[j];
if(User[i].PS[j]==p[j]) User[i].Pe_N++;
}
else if(User[i].PS[j]==-1) User[i].PS[j]=0;
}
if(flag) User[i].TS=-1;
}
//***********
//接下来排序,根据题目,排序权衡从主到次为:总分,完美解决数量,id序号
//***********
sort(User_Sort+1,User_Sort+N+1,cmp);
int rank=1;
int Srank=0;//same rank
for(i=1;i<=N;i++){
k=User_Sort[i];
if(User[k].TS>=0){
if(i>1){
if(User[k].TS==User[User_Sort[i-1]].TS) Srank++;
else {
rank+=Srank+1;
Srank=0;
}
}
printf("%d %05d %d ",rank,k,User[k].TS);
for(j=1;j<=K;j++){
if(User[k].PS[j]>=0) printf("%d",User[k].PS[j]);
else printf("-");
if(j<K) printf(" ");
else printf("\n");
}
}
}
return 0;
}