PAT-A 1075 PAT Judge (25 分)

1075 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 (≤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 -

分析:

坑点:用户未提交的题目的数量加上编译未通过的数量为题目数量时,不进行输出。

将每个用户声明为一个结构体,每个用户的属性有:id、排名、k道题目的得分(初始化全部为-2表示未提交),未提交的题目的数量,编译未通过的题目的数量,总分,得满分的题目数量。按照题目要求进行排序即可。输出时注意坑点。

#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

typedef struct user
{
    int id;
    int score[5];//每道题目的最高得分,-1表示编译未通过,-2表示未提交
    int rank;
    int nosubmit = 0;//未提交的数量
    int nocompile = 0;//未通过编译的数量
    int total_score = 0;//total score
    int perfect = 0;//perfect number
    user()
    {   //初始化所有题目为未提交
        fill(score, score + 5, -2);
    }
}user;

bool cmp(user a, user b)
{
    if(a.total_score != b.total_score)
        return a.total_score > b.total_score;//优先按照总分排序
    else if(a.perfect != b.perfect)
        return a.perfect > b.perfect;//再次按照满分题目数量排序
    else
        return a.id < b.id;
}
int main()
{
    int n, k, m;
    scanf("%d %d %d", &n, &k, &m);

    int full[k];//保存每道题目满分
    for(int i = 0; i < k; i++)
        scanf("%d", full + i);//输入每道题目的满分

    user all[n + 1];//存储n个用户,0号不用,调用构造函数
    for(int i = 1; i < n + 1; i++)
        all[i].id = i;//设置每个用户的id
    //处理每个提交的问题,更新用户状态
    int uid, pid, pscore;
    for(int i = 0; i < m; i++)
    {
        scanf("%d %d %d", &uid, &pid, &pscore);
        pid -= 1;//将题目编号转换为数组下标
        if(all[uid].score[pid] != full[pid] && pscore == full[pid])
            all[uid].perfect++;//某道题第一次满分时,将满分题目数量加1
        if(all[uid].score[pid] < pscore)
            all[uid].score[pid] = pscore;//取提交的最高分
    }
    //计算总分和未提交以及编译未通过的数量
    for(int i = 1; i < n + 1; i++)
    {
        for(int j = 0; j < k; j++)
        {
            if(all[i].score[j] == -2)//该题目该用户未提交
                all[i].nosubmit++;
            else if(all[i].score[j] == -1)//该题目未编译通过
                all[i].nocompile++;
            else
                all[i].total_score += all[i].score[j];
        }
    }
    sort(all + 1, all + n + 1, cmp);//对所有用户进行排序
    //计算排名
    int nowRank;
    for(int i = 1; i < n + 1; i++)
    {
        if(i == 1)
            nowRank = 1;
        else if(all[i].total_score != all[i - 1].total_score)
            nowRank = i;//如果分数和前一个用户不同,排名为他在序列中的序号
        all[i].rank = nowRank;
    }
    //输出用户信息
    for(int i = 1; i < n + 1; i++)
    {
        if(all[i].nocompile + all[i].nosubmit < k)//这里是坑点
        {
            printf("%d %05d %d", all[i].rank, all[i].id, all[i].total_score);
            for(int j = 0; j < k; j++)
            {
                if(all[i].score[j] == -2)//未提交
                    printf(" -");
                else if(all[i].score[j] == -1)//未通过编译
                    printf(" 0");
                else
                    printf(" %d", all[i].score[j]);
            }
            printf("\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38127801/article/details/86487287