问题描述:
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
输入的时候做一次排序并记录考场内Rank,最后再整体做一次排序以得到整体排名。不要忘了如果出现并列的话在并列之后的人要根据并列的人数顺延排名。
#include <bits/stdc++.h>
//#include <cstring>
using namespace std;
typedef struct{
string id;
int grade;
int loc;
int rank;
}RANK;
bool cmp(RANK a,RANK b){
if (a.grade!=b.grade) return a.grade>b.grade;
//else return strcmp(a.id.c_str(),b.id.c_str())<0;
else return a.id.compare(b.id)<0;
}
vector<RANK> ranklist;
int main(){
int n;
cin>>n;
int cur=0;
for (int a=1;a<=n;a++){
int people;
scanf ("%d",&people);
for (int b=0;b<people;b++){
RANK t;
cin>>t.id;
scanf ("%d",&t.grade);
t.loc=a;
ranklist.push_back(t);
}
sort (ranklist.begin()+cur,ranklist.end(),cmp);//内部排序
int interrank=1;
int same=1;
for (int b=cur;b<cur+people;b++){
if (b>cur) {
if (ranklist[b].grade<ranklist[b-1].grade) {
interrank+=same;//分数不同时之前的排名加上之前分数相同的人数即为当前的排名
same=1;//然后将same置为1
}
else same++;//分数相同时same++
ranklist[b].rank=interrank;//记录interrank
}
else {
ranklist[b].rank=interrank;
}
}
cur+=people;
}
sort (ranklist.begin(),ranklist.end(),cmp);//整体排序
cout<<cur<<endl;
int rank=1;
int thesame=1;
for (int pos=0;pos<ranklist.size();pos++){//同理
printf ("%s ",ranklist[pos].id.c_str());
if (pos) {
if (ranklist[pos].grade<ranklist[pos-1].grade) {
rank+=thesame;
thesame=1;
}
else thesame++;
printf ("%d",rank);
}
else {
printf ("1");
}
printf (" %d %d\n",ranklist[pos].loc,ranklist[pos].rank);
}
}