PAT-AL 1025. PAT Ranking

1、知识点:排序
2、思路:本题不难,可以只对全部成绩排序一次。用cur_fin_rank记录前一个总排名,cur_loc_rank记录前一个区域排名。这里需要注意,如果用qsort排序,不能用long long保存register_number,因为比较函数返回的是int类型,比较long long会出错,卡在这,最后一个测试点会过不去。

/*用途:
**说明:
**算法:
*/

//#define LOCAL
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
#define MAXLEN 15
#define MAXNUM 30000+10
#define MAXN 100+10
struct testee{
    char reg[MAXLEN];
    int score, loc, fin_rank, loc_rank;
};
testee tes[MAXNUM];
int N, K;
int cur_loc_rank[MAXN];
int cur_loc_ind[MAXN];

int cmp(const void* a, const void* b);
int main()
{
#ifdef LOCAL
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
#endif

    scanf("%d", &N);
    int num = 0;
    for(int i=0; i<N; i++){
        scanf("%d", &K);
        for(int j=0; j<K; j++){
            scanf("%s %d", tes[num].reg, &(tes[num].score));
            tes[num++].loc = i+1;
        }
    }

    qsort(tes, num, sizeof(testee), cmp);  //对所有成绩排序
    int cur_fin_rank = 0;
    printf("%d\n", num);    
    for(int i=0; i<num; i++){
        if(!cur_fin_rank){   //这里只会执行一次
            tes[i].fin_rank = 1;
            cur_fin_rank = 1;
        }
        else{
            if(tes[i].score == tes[i-1].score)  // 与前一个人成绩相同
                tes[i].fin_rank = tes[i-1].fin_rank;
            else  //成绩不同,最终排名就是下标i+1
                tes[i].fin_rank = i+1;
        }

        int loc = tes[i].loc;  //记录当前区域号
        if(!cur_loc_rank[loc]){
            tes[i].loc_rank = 1;
            cur_loc_rank[loc] = 1;
        }
        else{
            cur_loc_rank[loc]++;
            if(tes[i].score == tes[ cur_loc_ind[loc] ].score )
                tes[i].loc_rank = tes[ cur_loc_ind[loc] ].loc_rank;
            else
                tes[i].loc_rank = cur_loc_rank[loc];
        }
        cur_loc_ind[loc] = i;  //更新当前区域的最新下标
        printf("%s %d %d %d\n", tes[i].reg, tes[i].fin_rank, loc, tes[i].loc_rank);
    }

    return 0;
}

int cmp(const void* a, const void* b)
{
    if((*(testee*)a).score == (*(testee*)b).score)
        return strcmp((*(testee*)a).reg, (*(testee*)b).reg);
    else
        return (*(testee*)b).score - (*(testee*)a).score;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值