1. 程式人生 > >【PAT甲級】1075 PAT Judge

【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;
}

總結

學習不息,繼續加油