PTA 10-排序5 PAT Judge 题目分析 次位优先的基数排序实现

20 篇文章 2 订阅
3 篇文章 0 订阅

PTA-mooc完整题目解析及AC代码库:PTA(拼题A)-浙江大学中国大学mooc数据结构全AC代码与题目解析(C语言)


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

题目分析

题意说明

给定用户对于某个题目的提交记录列表,对所有用户的得分情况进行排名。

输入说明

第一行给定的三个数分别为:用户总数、题目总数以及提交记录总数;

第二行给定各个题目的满分是多少;

第三行开始的每一行对应一次提交记录,每一次提交记录都有三个值:用户id、提交题目以及得分情况(此处,如果未编译通过,则得分为-1)

输出说明

输出每个用户排名情况,每一行要输出的内容包括:名次、用户id、总分以及各题目得分。名次由总分区分,总分越高名次低。

排序依据是:按名次升序,名次相同则按最佳提交题目数量(最佳提交题目数等于该用户满分题目数)降序,再相同则按用户id顺序排列。

此外,没有任何提交记录的用户以及没有任何编译通过的提交的用户不计入排名。

关键点分析

其实这道题目思路并不困难,就是需要注意的细节特别多,实现起来需要特别小心。总结起来有如下几个注意点:

  1. 用户编号是从00001开始的,题目编号是从1开始的,都不是从0开始的;
  2. 题目给定的题目总数最大为5,远远小于用户数量;
  3. 输出时,没有任何提交记录的用户以及没有任何编译通过的提交的用户不计入排名;
  4. 输出每个用户各题目得分情况时,没有提交记录的题目输出"-",提交过但未编译通过以及编译通过但是得分为0的题目输出均为0

解法分析

这道题我是一遍过的,但是花了大概三小时左右完善细节。我用的是基数排序+表排序(间接排序),用其他排序方法也可以,但是要考虑排序算法的稳定性。

主要就分成三步:读入数据、排序、输出排序结果。

(1)读入数据

这里我定义了一个结构体数组,用于在读入数据的过程中记录每个用户相关信息的变化,这些信息都是后面排序以及输出过程中需要用到的内容,包括:用户总分、每道题最高得分以及得满分的题目数量。

(2)排序并输出结果

我因为用的是次位排序的基数排序,为了避免多余的数组拷贝,我这里直接把后两步合为一步。

首先对得满分题目数建立桶并排序,然后再对总得分范围建立桶然后排序。(注意遍历桶内元素时的顺序)

我想说明一下我使用基数排序的原因,首先因为基数排序是一个稳定的算法,其次因为本题中题目数量远远小于用户数量,而基数排序的时间复杂度为O(P(N+B)),这里的P趟数只有两趟,而B桶的个数在第一趟时最多也只能为6(因为该题的给定题目数最大为5),而第二趟时桶的个数与所有题目总分的取值范围有关,同样因为给定题目数很少,故分数范围也一定远小于用户数N,所以对此题使用基数排序得到的时间复杂度可近似看作O(N)。


代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct User_Info *Ptr_User_Info;
struct User_Info {
    int total_score;    // 总分,-1表示没有提交过或所有提交均未通过编译
    int score[5];   // 各题得分,-1表示提交但未通过编译,-2表示未提交
    int perfect_num;    // 最佳提交题目数
};
typedef Ptr_User_Info User_Info_Arr;

int user_num, problem_num;
User_Info_Arr info_arr;
int problem_mark[5];    // 存储各题目总分

/* ——————桶排序所需数据结构定义开始—————— */
/* 桶元素结点 */
typedef struct Node *PtrToNode;
struct Node {
    int key;
    PtrToNode next;
};

/* 桶头结点 */
struct HeadNode {
    PtrToNode head, tail;
};
typedef struct HeadNode *Bucket;
/* ——————桶排序所需数据结构定义结束—————— */

void solve();

void ReadData();    // 读取输入并初始化各数据
void LSDRadixSort_Print();    // 次位优先的基数排序,先对最佳答题数进行排序,再对总分进行排序,之后输出结果

int main()
{
    solve();

    return 0;
}

void solve()
{
    ReadData();
    LSDRadixSort_Print();
    free(info_arr);
}

void ReadData()
{
    int submission_num, i, j, user_id, problem_id, partial_score_obtained;

    scanf("%d %d %d", &user_num, &problem_num, &submission_num);
    for (i = 0; i < problem_num; ++i)
        scanf("%d", &problem_mark[i]);
    info_arr = (User_Info_Arr)malloc(user_num * sizeof(struct User_Info));
    // 初始化各用户的各题目得分为-2,总分为-1,即未提交状态
    for (i = 0; i < user_num; ++i) {
        for (j = 0; j < problem_num; ++j)
            info_arr[i].score[j] = -2;
        info_arr[i].total_score = -1;
        info_arr[i].perfect_num = 0;
    }
    for (i = 0; i < submission_num; ++i) {
        scanf("%d %d %d", &user_id, &problem_id, &partial_score_obtained);
        --user_id; --problem_id;
        if (info_arr[user_id].score[problem_id] == -2) { // 如果该题目未提交过,则只要该题目有提交记录,则均初始化为提交但未编译通过状态
            info_arr[user_id].score[problem_id] = -1;
        }
        if (info_arr[user_id].score[problem_id] < partial_score_obtained) {    // 提交编译通过
            if (info_arr[user_id].total_score < 0)  // 若该用户之前没有提交记录或没有任何编译通过提交,则将其置为有编译通过提交,即总分不小于0
                info_arr[user_id].total_score = 0;
            if (info_arr[user_id].score[problem_id] == -1)  // 若该用户该题的提交之前未编译通过,则此处将其置为0分
                info_arr[user_id].score[problem_id] = 0;
            info_arr[user_id].total_score += (partial_score_obtained - info_arr[user_id].score[problem_id]);
            if (partial_score_obtained == problem_mark[problem_id] && info_arr[user_id].score[problem_id] != partial_score_obtained)
                ++(info_arr[user_id].perfect_num);
            info_arr[user_id].score[problem_id] = partial_score_obtained;
        }
    }
}

void LSDRadixSort_Print()
{
    int i, j, total_score, rank, parallel_num;
    Bucket perfectBucket, scoreBucket;
    PtrToNode tmp;

    // 1. 先对最佳答题数建桶排序
    perfectBucket = (Bucket)malloc((problem_num + 1) * sizeof(struct HeadNode));
    for (i = 0; i <= problem_num; ++i)
        perfectBucket[i].head = perfectBucket[i].tail = NULL;
    for (i = 0; i < user_num; ++i) {
        if (info_arr[i].total_score < 0) continue;  // 如果该用户总分小于0,即没有提交或没有编译通过的提交,则不计入排序
        tmp = (PtrToNode)malloc(sizeof(struct Node));
        tmp->key = i;
        tmp->next = NULL;
        if (perfectBucket[info_arr[i].perfect_num].head == NULL)
            perfectBucket[info_arr[i].perfect_num].head = perfectBucket[info_arr[i].perfect_num].tail = tmp;
        else {
            perfectBucket[info_arr[i].perfect_num].tail->next = tmp;
            perfectBucket[info_arr[i].perfect_num].tail = tmp;
        }
    }

    // 2. 再对总得分建桶排序
    total_score = 0;
    for (i = 0; i < problem_num; ++i)   // 计算满分分数
        total_score += problem_mark[i];
    scoreBucket = (Bucket)malloc((total_score + 1) * sizeof(struct HeadNode));
    for (i = 0; i <= total_score; ++i)
        scoreBucket[i].head = scoreBucket[i].tail = NULL;
    for (i = problem_num ; i >= 0; --i) {
        while (perfectBucket[i].head) {
            tmp = perfectBucket[i].head;
            perfectBucket[i].head = perfectBucket[i].head->next;
            tmp->next = NULL;
            if (scoreBucket[info_arr[tmp->key].total_score].head == NULL)
                scoreBucket[info_arr[tmp->key].total_score].head = scoreBucket[info_arr[tmp->key].total_score].tail = tmp;
            else {
                scoreBucket[info_arr[tmp->key].total_score].tail->next = tmp;
                scoreBucket[info_arr[tmp->key].total_score].tail = tmp;
            }
        }
    }

    // 3. 输出结果
    rank = 1;
    for (i = total_score; i >= 0; --i) {
        parallel_num = 0;   // 计算同名次的人数
        while (scoreBucket[i].head) {
            tmp = scoreBucket[i].head;
            scoreBucket[i].head = scoreBucket[i].head->next;
            printf("%d %05d %d", rank, tmp->key + 1, info_arr[tmp->key].total_score);
            for (j = 0; j < problem_num; ++j) {
                if (info_arr[tmp->key].score[j] == -2)
                    printf(" -");
                else if (info_arr[tmp->key].score[j] == -1)
                    printf(" 0");
                else
                    printf(" %d", info_arr[tmp->key].score[j]);
            }
            printf("\n");
            free(tmp);
            ++parallel_num;
        }
        rank += parallel_num;
    }

    free(perfectBucket);
    free(scoreBucket);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值