十、排序(下):PAT Judge(K最小,有0分不通过的解决方法)

该博客介绍了PAT排名算法的实现,包括输入输出规格,样例输入输出,并强调了解题思路。重点在于处理用户无提交或所有提交失败以及部分得分情况,确保排名的准确性和稳定性。采用基数排序,优先处理每道题的成绩,然后是满分题目数量,最后是用户ID。博客提到了几个关键细节,如排除无提交或全0分的学生,以及如何处理部分得分的问题。
摘要由CSDN通过智能技术生成

题目描述

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 (≤10​4​​ ), the total number of users, K (≤5), the total number of problems, and M (≤10​5​​ ), 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 -

代码

#include<stdio.h>
#include<malloc.h>
#define MaxN 10001
#define MaxK 6
#define MaxM 100001

int Full_Score[MaxK];
int K;

struct Score_Information{
	int id; /* 学生id */
	int wether; /* 是否有过提交 */ 
	int sub[5]; /* 标记哪一道题是提交过的 */ 
	int score[5]; /* 几道题的最好成绩 */
	int sum_score; /* 总分数 */
	int num_correct; /* 完全答对了几道题 */
}Sc[MaxN];

void Merge(struct Score_Information A[], struct Score_Information *temp, int L, int R, int RightEnd, int x)
{
	int LeftEnd = R - 1, tmp = L, Num = RightEnd - L + 1, i;
	if( x==1 ) /* 要求对num_correct排序 */{
		while( L <= LeftEnd && R <= RightEnd ){
			if( A[L].num_correct >= A[R].num_correct ) temp[tmp++] = A[L++];
			else 			   temp[tmp++] = A[R++];
		}
	}
	else /* 要求对sum_score排序 */{
		while( L <= LeftEnd && R <= RightEnd ){
			if( A[L].sum_score >= A[R].sum_score ) temp[tmp++] = A[L++];
			else 			   temp[tmp++] = A[R++];
		}
	}
	while( L <= LeftEnd )
		temp[tmp++] = A[L++];
	while( R <= RightEnd )
		temp[tmp++] = A[R++];
	for( i=0; i<Num; ++i,--RightEnd )
		A[RightEnd] = temp[RightEnd];
}

void Merge_pass(struct Score_Information A[], struct Score_Information *temp, int N, int length, int x)
{
	int i, j;
	for( i=0; i<=N-2*length; i+=2*length )
		Merge( A, temp, i, i+length, i+2*length-1 , x );
	if( i+length < N )
		Merge( A, temp, i, i+length, N-1 , x );
	else
		for( j=i; j!=N; ++j )
			temp[j] = A[j];
}

void Merge_sort(struct Score_Information A[], int N, int x)
{
	int length;
	struct Score_Information *temp;
	length = 1;
	temp = (struct Score_Information *)malloc(N*sizeof(struct Score_Information));
	if( temp!=NULL ){
		while( length < N ){
			Merge_pass( A, temp, N, length, x );
			length *= 2;
			Merge_pass( temp, A, N, length, x );
			length *= 2;
		}
		free( temp );
	}
}

void LSD_Sort(struct Score_Information Sc[], int N)
{/* Sc[]数组中元素包含了每个id的信息,但是数组中有信息的元素不是连续的 */
 /* 可能Sc[1]和Sc[3]有信息,Sc[2]没有信息,因为这个学生没有提交答案,或者答案分数为-1 */
	int i, j=0, k;
	struct Score_Information Sco[MaxN];
	for(i=0;i<=N;++i){ /* 整理一个连续的数组,这个连续数组已经按id从小到大排序了 */
		if(Sc[i].wether)
			Sco[j++] = Sc[i];
	}
	int Sco_N = j; /* 整理后的用来排序的数组元素个数 */
	
	/* 按答对多少题从大到小排序,一定要使用稳定排序。这里使用归并 */
	Merge_sort(Sco, j, 1);
	/* 按总得分从大到小排序,一定要使用稳定排序,这里使用归并 */
	Merge_sort(Sco, j, 2);
	int Order[j];
	Order[0] = 1;
	for(k=1;k<j;++k){
		if(Sco[k].sum_score==Sco[k-1].sum_score)
			Order[k] = Order[k-1];
		else
			Order[k] = k+1;
		}
	for(i=0;i!=j;++i){
		
		printf("%d %05d %d ",Order[i],Sco[i].id,Sco[i].sum_score);
		for(k=0;k<K-1;++k){
			if(Sco[i].score[k]>0 || Sco[i].sub[k]>0 )
				printf("%d ",Sco[i].score[k]);
			else printf("- ");
		}
		if(Sco[i].score[K-1]>0 || Sco[i].sub[K-1]>0 ) printf("%d\n",Sco[i].score[K-1]);
		else printf("-\n");
	}
}

int main()
{
	/* 读取数据 */
	int N, M;
	scanf("%d%d%d\n",&N,&K,&M);
	int i, j, Num_Student, Num_Quesition, temp_score;
	int Max_id = -1; 
	for( i=0; i!=K; ++i )
		scanf("%d",&Full_Score[i]);
	for( i=0; i!=M; ++i ){
		scanf("%d%d",&Num_Student,&Num_Quesition);
		Sc[Num_Student-1].sub[Num_Quesition-1] = 1;
		scanf("%d",&temp_score); 
		if(temp_score>=0) 
			Sc[Num_Student-1].wether = 1; /* 标记该学生曾经提交过答案 */ 
		if(Num_Student-1>Max_id) 
			Max_id =  Num_Student-1; /* 记录下有成绩的最大学生位置 */
		Sc[Num_Student-1].id = Num_Student; /* 得到学生id,从1开始 */
		if(temp_score>Sc[Num_Student-1].score[Num_Quesition-1]) /* 得到该学生某一道题的最佳成绩。成绩为-1则直接被忽略 */
			Sc[Num_Student-1].score[Num_Quesition-1] = temp_score; /* 学生和题号都是从0开始存储的 */
		Sc[Num_Student-1].sum_score = 0;
		Sc[Num_Student-1].num_correct = 0;
		for( j=0; j!=K; ++j ){
			Sc[Num_Student-1].sum_score += Sc[Num_Student-1].score[j]; /* 更新总得分 */
			if( Sc[Num_Student-1].score[j] == Full_Score[j] )
				++Sc[Num_Student-1].num_correct; /* 更新完全正确的题目数量 */
		}
	}
	
	/* 基数排序,次位优先。从次到主的顺序为:学生id、学生答对的题目数、学生总成绩 */
	LSD_Sort(Sc, Max_id);
	
	return 0;
}


解题思路

整体思路是比较简单的。建立结构体,主要的成员是学生id,每道题的成绩score[],满分题目数量num_correct这个学生的总成绩sum_score。使用基数排序,次位优先,从次到主的顺序是idnum_correctsum_score。排序算法必须是稳定的。

需要注意的细节:
1.如果一个学生没有任何提交,或者所有提交结果都是-1,那么这个学生不进入排序。(示例中的学生6)
2.如果一个学生进入了排序,提交结果是-1的题目最后显示应该为0.(示例中的学生5)
3.如果一个学生所有提交结果都是0,那么没有违反上面的条件1,应该进入排序。(如果不进入排序,测试点2“**K最小,有0分”**和最后一个测试点都不能通过)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值