PAT甲级 1025

PAT甲级 1025

题目 PAT Ranking

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:
registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

解析

n代表数据,k代表每组数据包含几个学生,输入学生的学号、成绩。输出在所有组中每个学生的学号、所有学生中的排名、第几组数据、组中排名。

代码

#include<bits/stdc++.h>

#define INF 1<<29
using namespace std;

struct student {
    char id[15];
    int score;
    int all_num;
    int classroom;
    int local_num;
};

student res[30050];

bool cmp(student s1, student s2) {

    if (s1.score != s2.score) return s1.score > s2.score;
    else return strcmp(s1.id, s2.id) < 0;
}

void getRank(int begin, int end,bool flag) {
    int l = 1;
    for (int k = begin; k <= end; ++k) {
        if (flag)
            res[k].local_num = l;
        else
            res[k].all_num = l;
        int l2 = 1;
        for (int j = k + 1; j <= end; ++j) {
            if (res[k].score == res[j].score) {
                if (flag)
                    res[j].local_num = l;
                else
                    res[j].all_num = l;
                ++k;++l2;
            } else {
                break;
            }
        }
        l += l2;
    }
}

void pat1025() {
    int n, index = 0;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int classroom_size;
        cin >> classroom_size;
        for (int j = 1; j <= classroom_size; ++j) {
            char id[15];
            int score;
            cin >> id;
            cin >> score;
            student *s = new student;
            strcpy(s->id, id);
            s->score = score;
            s->classroom = i;
            res[index + j] = *s;
        }
        sort(res + index + 1, res + index + classroom_size + 1, cmp);
//        for (int y = index + 1; y <= index + classroom_size; ++y) {
//            cout <<"--> "<< res[y].id << " " << res[y].all_num << " " << res[y].classroom << " " << res[y].local_num << endl;
//        }
        getRank(index + 1, index + classroom_size,true);
//        for (int y = index + 1; y <= index + classroom_size; ++y) {
//            cout << res[y].id << " " << res[y].all_num << " " << res[y].classroom << " " << res[y].local_num << endl;
//        }
        index += classroom_size;
    }
    sort(res + 1, res + index + 1, cmp);
    getRank(1,index,false);
    cout << index << endl;
    for (int i = 1; i <= index; ++i) {
        cout << res[i].id << " " << res[i].all_num << " " << res[i].classroom << " " << res[i].local_num << endl;
    }
}

int main() {
    pat1025();
    return 0;
}
上一篇:PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)


下一篇:EBCDIC 1025 俄语