1025 PAT Ranking 双重排序

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 afer 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

 

题目意思:模拟一下PAT考试的排名机制,n个考场,每个考场若干学生,给出每个考场学生的登记编号和考试分数,确定排名,输出所有考生的登记编号、总排名、考场号、考场内排名。这里如果出现了相同的分数,那么登记编号小的优先,但排名确实并列的,假如两个人都是第一名,那么次于他们的那个人将会是第三名。

解题思路:先在本考场内排名,记录在本考场内的名次,再到总的当中排名,记录最终的排名。

/*
双重排序
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
struct stu
{
    string id;
    int score;
    int final_rank;//最终排名
    int local;//所在考场号
    int local_rank;//考场内排名
};
stu a[110*310];
int my_cmp(stu a,stu b)
{
    if(a.score != b.score)
    {
        return a.score > b.score;
    }
    else//成绩相同,考号小的靠前
    {
        return a.id < b.id;
    }
}
int main()
{

    int n,m;
    int i,j;
    int cnt=0;
    cin>>n;
    for(i=1; i<=n; i++)
    {
        cin>>m;
        for(j=0; j<m; j++)
        {
            cin>>a[cnt].id>>a[cnt].score;
            a[cnt].local=i;//所在考场
            cnt++;
        }
        sort(a+cnt-m,a+cnt,my_cmp);//此考场学生下标为[cnt-k,cnt-1]
        int loc_rank=1;
        int pre=a[cnt-m].score;
        for(j = cnt - m; j < cnt; j++)
        {
            if(a[j].score != pre)//与前一个同分
            {
                loc_rank = j-(cnt-m) + 1;
                pre = a[j].score;
            }
            a[j].local_rank = loc_rank;
        }
    }
    cout << cnt << endl;
    sort(a,a+cnt,my_cmp);
    int fin_rank=1;
    int pre=a[0].score;
    for(j = 0; j < cnt; j++)
    {
        if(a[j].score != pre)//与前一个同分
        {
            fin_rank = j + 1;
            pre = a[j].score;
        }
        a[j].final_rank = fin_rank;
        cout<< a[j].id <<' '<< a[j].final_rank <<' '<< a[j].local <<' '<< a[j].local_rank <<endl;
    }
    return 0;
}

 

posted @ 2019-08-31 20:08  王陸  阅读(330)  评论(0编辑  收藏  举报