【PAT甲级】1075 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 -

题目大意

这题就是给出总人数n、题目数k、总提交数m,然后给出k道题的满分,接着给出m个提交,每个提交中包括人的id、题目编号以及这题的得分,得分为-1表示编译没过。然后最后输出所有至少有一道题过编译的人的排名

个人思路

这题还是排序算法的应用。设立学生信息结构体,接着填入信息,然后进行排序,然后输出。存储结构体数组我使用了vector,同时用我是用string存id的,因此还建立了一个map存储id到索引的映射。当然也可以直接建一个静态数组用下标存,只是最近想多练习stl。

有一个注意点就是只有没提交的在输出时才是'-',如果只是编译不通过输出的还是0;

代码实现

#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define eps 1e-8
#define INF 0x7FFFFFFF

using namespace std;

// 学生信息结构体
struct Student{
    string id;
    int score[6], sum, rank, perfect_num;
};

// 比较函数优先级:总分 > 完美答案个数 > id大小
bool cmp(Student s1, Student s2) {
    if (s1.sum != s2.sum) return s1.sum > s2.sum;
    else if (s1.perfect_num != s2.perfect_num) return s1.perfect_num > s2.perfect_num;
    else return s1.id < s2.id;
}

int main() {
    // 输入
    int n, k, m, full_score[6];
    cin >> n >> k >> m;
    for (int i = 1; i <= k; i ++) {
        cin >> full_score[i];
    }
    
    // 对m个提交进行处理
    map<string, int> idx; // 存储id到索引的映射
    vector<Student> students; // 动态数组存储有提交的学生
    int cnt = 0; // 有提交学生的个数
    for (int i = 0; i < m; i ++) {
        string id;
        int que_id, que_score;
        cin >> id >> que_id >> que_score;
        // 该学生的第一次提交,初始化学生信息,加入vector
        if (idx[id] == 0) {
            Student s;
            s.id = id;
            for (int j = 0; j < 6; j ++) {
                s.score[j] = -2; // -2代表初始时未提交
            }
            s.perfect_num = 0;
            s.sum = 0;
            students.push_back(s);
            idx[id] = ++cnt;
        }
        // 如果提交的得分比原得分高
        if (que_score > students[idx[id]-1].score[que_id]) {
            students[idx[id]-1].score[que_id] = que_score;
            if (que_score == full_score[que_id]) students[idx[id]-1].perfect_num ++;
        }
    }
    
    // 计算每个学生的总得分
    for (int i = 0; i < cnt; i ++) {
        for (int j = 1; j <= k; j ++) {
            if (students[i].score[j] > 0) students[i].sum += students[i].score[j];
        }
    }
    
    // 排序
    sort(students.begin(), students.end(), cmp);
    
    // 排名
    students[0].rank = 1;
    for (int i = 1; i < cnt; i ++) {
        if (students[i].sum == students[i-1].sum) students[i].rank = students[i-1].rank;
        else students[i].rank = i+1;
    }
    
    // 输出
    for (int i = 0; i < cnt; i ++) {
        Student s = students[i];
        // 如果所有的提交都没有过编译,则跳过不输出
        if (s.sum == 0) {
            bool out = true;
            for (int j = 1; j <= k; j ++) {
                if (s.score[j] >= 0) {
                    out = false;
                    break;
                }
            }
            if (out) continue;
        }
        cout << s.rank << " " << s.id << " " << s.sum;
        for (int j = 1; j <= k; j ++) {
            if (s.score[j] >= 0) cout << " " << s.score[j];
            else if (s.score[j] == -1) cout << " 0";
            else cout << " -";
        }
        cout << endl;
    }
    return 0;
}

在添加注释重新提交后我发现竟然超时了最后一个样例,再提交一次又不超时了,说明这个比较危险处在超时的边缘。因此我又把所有的cin和cout改成了scanf和printf,然后就不会超时啦。

#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define eps 1e-8
#define INF 0x7FFFFFFF

using namespace std;

// 学生信息结构体
struct Student{
    string id;
    int score[6], sum, rank, perfect_num;
};

// 比较函数优先级:总分 > 完美答案个数 > id大小
bool cmp(Student s1, Student s2) {
    if (s1.sum != s2.sum) return s1.sum > s2.sum;
    else if (s1.perfect_num != s2.perfect_num) return s1.perfect_num > s2.perfect_num;
    else return s1.id < s2.id;
}

int main() {
    // 输入
    int n, k, m, full_score[6];
    scanf("%d %d %d", &n, &k, &m);
    for (int i = 1; i <= k; i ++) {
        scanf("%d", &full_score[i]);
    }
    
    // 对m个提交进行处理
    map<string, int> idx; // 存储id到索引的映射
    vector<Student> students; // 动态数组存储有提交的学生
    int cnt = 0; // 有提交学生的个数
    for (int i = 0; i < m; i ++) {
        char id_str[10];
        string id;
        int que_id, que_score;
        scanf("%s %d %d", id_str, &que_id, &que_score);
        id = id_str;
        // 该学生的第一次提交,初始化学生信息,加入vector
        if (idx[id] == 0) {
            Student s;
            s.id = id;
            for (int j = 0; j < 6; j ++) {
                s.score[j] = -2; // -2代表初始时未提交
            }
            s.perfect_num = 0;
            s.sum = 0;
            students.push_back(s);
            idx[id] = ++cnt;
        }
        // 如果提交的得分比原得分高
        if (que_score > students[idx[id]-1].score[que_id]) {
            students[idx[id]-1].score[que_id] = que_score;
            if (que_score == full_score[que_id]) students[idx[id]-1].perfect_num ++;
        }
    }
    
    // 计算每个学生的总得分
    for (int i = 0; i < cnt; i ++) {
        for (int j = 1; j <= k; j ++) {
            if (students[i].score[j] > 0) students[i].sum += students[i].score[j];
        }
    }
    
    // 排序
    sort(students.begin(), students.end(), cmp);
    
    // 排名
    students[0].rank = 1;
    for (int i = 1; i < cnt; i ++) {
        if (students[i].sum == students[i-1].sum) students[i].rank = students[i-1].rank;
        else students[i].rank = i+1;
    }
    
    // 输出
    for (int i = 0; i < cnt; i ++) {
        Student s = students[i];
        // 如果所有的提交都没有过编译,则跳过不输出
        if (s.sum == 0) {
            bool out = true;
            for (int j = 1; j <= k; j ++) {
                if (s.score[j] >= 0) {
                    out = false;
                    break;
                }
            }
            if (out) continue;
        }
        printf("%d %s %d", s.rank, s.id.data(), s.sum);
        for (int j = 1; j <= k; j ++) {
            if (s.score[j] >= 0) printf(" %d", s.score[j]);
            else if (s.score[j] == -1) printf(" 0");
            else printf(" -");
        }
        printf("\n");
    }
    return 0;
}

总结

学习不息,继续加油

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值