【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.

翻译:编程能力测试(PAT)由浙江大学计算机科学与技术学院( 教练,我要上浙大! )每次测试需要在不同场地同时举行,并且排名表必须在测试后立刻合并。现在你的任务是写一个程序去正确的合并所有排名表并且生成最终的排名。

INPUT FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括一个正整数N(<=100),代表测试地点的数目。接下来的N个排名表,每一个开头都是一个正整数K (<=300),代表参试者的人数,接下来K行每行包括登记号(一个13位数字)和该生的总成绩。所有数字由空格隔开。

OUTPUT FORMAT

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.

翻译:对于每组输入数据,首先输出一行参试者的总人数。接着根据以下格式输出最终的排名表:
注册号 最终排名 考场号 考场排名

考场被标记为1-N。输出必须根据最终排名升序排序。得分相同的参赛者必须有同样的排名,并且必须按照注册号升序输出。


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


解题思路

定义一个学生结构体,用于保存数据,重写<号,用优先队列维护。注意输出时要判断与同场次的前一位比较,还要与总排名的前一位比较。数据可能为0,所以保存前一位成绩的数组初始化时要初始化为-1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
struct Stu{
    string id;
    int Time,score;
    Stu(string id,int t,int s):id(id),Time(t),score(s){}
    bool operator<(const Stu &a)const{
        return score==a.score?id>a.id:score<a.score;    
    }
}; 
priority_queue<Stu>stu;
int N,K,ccount=0;
int Tim[110][3];//0记录分数,1记录前一位的排名,2记录当前为第几个学生
int main(){
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%d",&K);
        ccount+=K;
        string s;
        int score;
        for(int j=0;j<K;j++){
            cin>>s>>score;
            stu.push(Stu(s,i,score));
        }
    }
    int tcount=0,preScore=-1,preCount=0;
    cout<<ccount<<endl;
    for(int i=0;i<N;i++)Tim[i][0]=-1;
    while(!stu.empty()){
        Stu temp=stu.top();stu.pop();
        int s=temp.score,t=temp.Time;
        Tim[t][2]++;
        tcount++;
        if(Tim[t][0]!=s){
            Tim[t][0]=s;
            Tim[t][1]=Tim[t][2];
        }
        if(s!=preScore){
            preScore=s;
            preCount=tcount;
        }
        cout<<temp.id<<" "<<preCount<<" "<<t+1<<" "<<Tim[t][1]<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值