10-排序5 PAT Judge
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 <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;//这里不能直接赋值为0.假设一种情况,某个id的提交全未通过编译,那这个id就不会被输出。这跟通过编译但成绩为0的性质是不一样的
}
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;
}/*如果一个题目只提交了一次且未通过编译,那这个题目输出时应该是0分,所以这里有问题。更新:没问题,在最后输出的时候考虑到了这种情况*/
}
}
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) { /*这里按照序号增序建桶。保证当两个id分数一样,满分题目书一样时,序号小的在上边*/
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);
}
这个题思路很绕,看了将近一整天才理解,很好的一个题。
但我刚看到这个题目时,我的思路是建立一个二维的数组,以这个题为例:
建立数组arr[8][6],第0行空着不用,随后1-7行代表7个用户。第0列是7个user_id,1-4列是4个题目的分数,第5列记录满分题目的数目。
其他的思路就跟代码中的一致了。
主要是我目前还不熟悉结构体的使用,这是很严重的问题!!!