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.

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


代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define Max 1001
using namespace std;

struct pat
{
    char num[14];//记录编号
    int sco,loca,locaran,ran;//sco记录成绩loca记录所属于location locaran记录所在location的排名 ran记录总排名
}tot[30000];//记录总的数据
int last[101];//所在location中上一个元素的下标
int ind[101];//每个location已经输出的元素个数
int no = 0;//总数据的下标
int n,k;
bool cmp(pat a,pat b)
{
    if(a.sco == b.sco)return strcmp(a.num,b.num) < 0;
    return a.sco > b.sco;
}
int main()
{
    cin>>n;
    for(int i = 1;i <= n;i ++)
    {
        cin>>k;
        last[i] = -1;//初始为-1,一开始不存在所在location 的上一个元素下标  i就是location编号
        for(int j = 0;j < k;j ++)
        {
            cin>>tot[no + j].num>>tot[no + j].sco;
            tot[no + j].loca = i;///记录location编号
        }
        no += k;//更新总下标
    }
    sort(tot,tot+no,cmp);
    cout<<no<<endl;
    cout<<tot[0].num<<' '<<1<<' '<<tot[0].loca<<' '<<++ ind[tot[0].loca]<<endl;
    tot[0].locaran = 1;
    tot[0].ran = 1;
    last[tot[0].loca] = 0;

    for(int i = 1;i < no;i ++)
    {
        if(tot[i].sco == tot[i - 1].sco)//跟前一个元素成绩相同,总排名就相同
        {
            tot[i].ran = tot[i - 1].ran;
        }
        else tot[i].ran = i + 1;//否则等于当前i + 1
        if(last[tot[i].loca] >= 0 && tot[i].sco == tot[last[tot[i].loca]].sco)//跟同一个location上一个元素成绩相同 则排名相同
        {
            tot[i].locaran = tot[last[tot[i].loca]].locaran;
            ++ ind[tot[i].loca];///当前元素所在location元素个数加1
        }
        else tot[i].locaran = ++ ind[tot[i].loca];//否则就是所在location总的元素个数
        last[tot[i].loca] = i;//更新last
        cout<<tot[i].num<<' '<<tot[i].ran<<' '<<tot[i].loca<<' '<<tot[i].locaran<<endl;
    }
}
原文地址:https://www.cnblogs.com/8023spz/p/7953868.html