PAT 甲级 1025 PAT Ranking (25分)

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.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
一个小难点: 
    100 100 99 则得分99的同学的排名为3,其实得分相同的人的排名相同比较容易实现,但是像99分这样同学的排名就直接是:他前面和他本身的人数

一个坑点:
    每个人的id最好不要用long long来存储,因为题目给的样例容易引导我们直接输出id数值,而忽略了id要始终保持13位,因此不够13位是要补0的
    int rank = 1;
    for (int i = 0; i < sum; ++i){
        if (i > 0 && stu[i].score != stu[i - 1].score)
            rank = i + 1;
        stu[i].real_rank = rank;    
    }  
#include<iostream>
#include<algorithm>
using namespace std;

struct student{
    long long id;
    int location;
    int score;
    int local_rank;
    int real_rank;
}stu[30010];

bool cmp(student stu1,student stu2){
    if (stu1.score != stu2.score)
        return stu1.score > stu2.score;
    else return stu1.id < stu2.id;
}

long long n,k,id,score,idx,sum;

int main(){
    
    cin>>n;
    for (int i = 0; i < n ; ++i){
        cin>>k;
        sum += k;
        int st = idx;
        for (int j = 1; j <= k; ++j){
            cin>>id>>score;
            stu[idx++] = {id,i + 1,score};
        }
        sort(stu + st,stu + st + k,cmp);
        
        int rank = 1;
        for (int j = st; j < st + k; ++j){
            if (j - st > 0 && stu[j].score != stu[j - 1].score)
                rank = j - st + 1;
            stu[j].local_rank = rank;    
        }
    }
    
    sort(stu,stu + sum,cmp);
    int rank = 1;
    for (int i = 0; i < sum; ++i){
        if (i > 0 && stu[i].score != stu[i - 1].score)
            rank = i + 1;
        stu[i].real_rank = rank;    
    }    
    
    cout<<sum<<endl;
    
    for (int i = 0; i < sum; ++i){
        printf("%013lld ",stu[i].id);
        cout<<stu[i].real_rank<<" "<<stu[i].location<<" "<<stu[i].local_rank<<endl;
    }
    
    return 0;
}
发布了423 篇原创文章 · 获赞 38 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_41514525/article/details/105106477