【PAT】A1025. PAT Ranking (25)

A1025. PAT Ranking (25)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct student{
    char id[14 // registration_number
    int score;
    int local_rank; // local_rank
    int group_num; // location_number
    int rank; // final_rank
}student[300000];

// 比较规则,分数优先比较,分数相同,比较ID
bool cmp(struct student s1, struct student s2){
    if(s1.score != s2.score)
        return s1.score > s2.score;
    else
        return strcmp(s1.id, s2.id) < 0;
}

int main(){
    int N, K, i, j, index = 0, _rank;
    scanf("%d", &N);
    for(i = 0; i < N; i++){
        scanf("%d", &K);
        // 输入第 i 组
        for(j = 0; j < K; j++){
            scanf("%s %d", student[index].id, &student[index].score);
            student[index].group_num = i + 1;
            index++;
        }
        // 排序第 i 组
        sort(student + index - K, student + index, cmp);
        _rank = 1;
        // 确定 local_rank
        for(j = index - K; j < index; j++){
            if(j > index - K){
                if(student[j].score == student[j - 1].score){
                    student[j].local_rank = student[j - 1].local_rank;
                    _rank++;
                    continue;
                }
            }
            student[j].local_rank = _rank;
            _rank++;
        }
    }

    printf("%d\n", index);

    // 总排序
    sort(student, student + index, cmp);
    // 确定 all_rank
    _rank = 1;
    for(i = 0; i < index; i++){
        if(i != 0){
            if(student[i].score == student[i - 1].score){
                student[i].rank = student[i - 1].rank;
                printf("%s %d %d %d\n", student[i].id, student[i].rank, student[i].group_num, student[i].local_rank);
                _rank++;
                continue;
            }
        }
        student[i].rank = _rank;
        printf("%s %d %d %d\n", student[i].id, student[i].rank, student[i].group_num, student[i].local_rank);
        _rank++;
    }

    return 0;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值