PAT甲题题解-1025. PAT Ranking (25)-排序

排序,求整体的排名和局部的排名
整体排序,for循环一遍
同时存储整体目前的排名和所在局部的排名即可

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
const int maxn=105;
const int maxk=305;
int local_Rank[maxn];  //local_Rank[i]表示第i个组目前的排名
int last_local_id[maxn]; //last_local_id[i]表示第i个组前一个在整体中的索引
struct Node{
    long long id;
    int score;
    int group;
    int ranks;
    int local_rank;
    bool operator<(const Node tmp)const{
        if(score==tmp.score)
            return id<tmp.id;
        else
            return score>tmp.score;
    }
}stu[maxn*maxk];
int main()
{
    int n,k;
    scanf("%d",&n);
    int cnt=0;
    long long id;
    int score;
    for(int i=1;i<=n;i++){
        scanf("%d",&k);
        for(int j=0;j<k;j++){
            scanf("%lld %d",&id,&score);
            stu[cnt].id=id;
            stu[cnt].score=score;
            stu[cnt].group=i;
            cnt++;
        }
    }
    printf("%d\n",cnt);
    sort(stu,stu+cnt);
    memset(local_Rank,0,sizeof(local_Rank));
    int global_rank=0; //整体排名
    for(int i=0;i<cnt;i++){
        if(global_rank==0){
            stu[i].ranks=1;
            global_rank=1;
        }
        else{
            if(stu[i].score==stu[i-1].score){
                stu[i].ranks=stu[i-1].ranks;
                global_rank++;
            }
            else{
                global_rank++;
                stu[i].ranks=global_rank;
            }
        }
        int group=stu[i].group;
        if(local_Rank[group]==0){
            stu[i].local_rank=1;
            local_Rank[group]=1;
            last_local_id[group]=i;
        }
        else{
            int id=last_local_id[group];
            if(stu[i].score==stu[id].score){
                stu[i].local_rank=stu[id].local_rank;
                local_Rank[group]++;
            }
            else{
                local_Rank[group]++;
                stu[i].local_rank=local_Rank[group];
                last_local_id[group]=i;
            }
        }
        printf("%013lld %d %d %d\n",stu[i].id,stu[i].ranks,stu[i].group,stu[i].local_rank);
    }
    return 0;
}
View Code

 

posted @ 2017-04-19 19:55  辰曦~文若  阅读(266)  评论(0编辑  收藏  举报