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

 

不难,比之前电话单那题考排序少了好多坑,本题唯一的问题在英文阅读水平。或者说是细心吧。考场号(我全程理解为提交的用户id)是最后一个排序依据

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
struct Regist{
string id;
double score;//未说明类型 保险起见
int gId,gIndex,Index;//groupId组号 groupIndex组排名 Index:总排名
}reg[30010];

//题目要审清 末尾的 registration numbers. 就是测试id第一次输出的那个 结构体的那个id 也是排序依据 不是输入顺序
bool compare1(Regist a,Regist b){
if(a.score!=b.score) return a.score>b.score;
return a.id<b.id;
}
bool compare2(Regist a,Regist b){
if (a.gId!=b.gId) return a.gId<b.gId;
if(a.score!=b.score) return a.score>b.score;
return a.id<b.id;
}
int N,K,Count=0;
int main(){
//freopen("in.txt","r",stdin);
cin>>N;
for(int i=1;i<=N;i++){
cin>>K;
while(K--){
cin>>reg[Count].id>>reg[Count].score;
reg[Count].gId=i;
Count++;
}
}

//要输出的信息都写到结构体成员变量里 这样最安全 算出来就存好了 接下来又可以肆无忌惮地打断原结构体数组了
sort(reg,reg+Count,compare2);
int nowIndex=0;
reg[0].gIndex=++nowIndex;
for(int i=1;i<Count;i++){
if(reg[i].gId!=reg[i-1].gId) nowIndex=0;//换组
reg[i].gIndex=++nowIndex;
if(reg[i].gIndex!=1&&reg[i].score==reg[i-1].score){
reg[i].gIndex=reg[i-1].gIndex;
}
}

//先求组号 再求总排名 因为最后按总排名输出
sort(reg,reg+Count,compare1);
for(int i=0;i<Count;i++){//计算总排名 主要处理分数相同排名相同
reg[i].Index=i+1;
if(i>0&&reg[i].score==reg[i-1].score){
reg[i].Index=reg[i-1].Index;//前面的肯定先排好了 不同的照旧下标+1
}
}

//就这样freopen和输出一直先写好放着 写一点执行一次 很容易测试debug
cout<<Count<<endl;
for(int i=0;i<Count;i++){
cout<<reg[i].id<<" "<<reg[i].Index<<" "<<reg[i].gId<<" "<<reg[i].gIndex<<endl;
}
return 0;
}