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
My code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class student
{
public:
string id;
int grade;
int location;
int localRank;
int Rank;
};
const int MAX = 100*300;
void swap(student **S, int i, int j)
{
student *tmp = S[i];
S[i] = S[j];
S[j] = tmp;
}
bool largeThan(string str1, string str2, int N)
{
for(int i=0; i<N; i++){
if(str1[i]>str2[i]) return true;
else if(str1[i]<str2[i]) return false;
}
return false;
}
void quickSort(student **S, int left, int right)
{
if(right-left+1<2) return;
int i=left, j=right-1;
int midian = (right+left)/2;
string pivot = S[midian]->id;
swap(S, midian, right);
while(i<j){
while(largeThan(pivot, S[i]->id, 13)) i++;
while(largeThan(S[j]->id, pivot, 13)) j--;
if(i<j){
swap(S, i, j);
}
}
if(largeThan(S[i]->id, S[right]->id, 13)) swap(S, i, right);
quickSort(S, left, i-1);
quickSort(S, i+1, right);
}
void merge(student **S, int left, int mid, int right, student **tmp)
{
int i=left, j=mid+1;
int rear = -1;
while(i<=mid && j<=right){
if(S[i]->grade > S[j]->grade) {
tmp[++rear] = S[i];
i++;
}
else if(S[i]->grade < S[j]->grade){
tmp[++rear] = S[j];
j++;
}else{
if(largeThan(S[i]->id, S[j]->id, 13)){
tmp[++rear] = S[j];
tmp[++rear] = S[i];
i++; j++;
}else{
tmp[++rear] = S[i];
tmp[++rear] = S[j];
i++; j++;
}
}
}
while(i<=mid){
tmp[++rear] = S[i];
i++;
}
while(j<=right){
tmp[++rear] = S[j];
j++;
}
for(int i=0; i<=rear; i++){
S[left+i] = tmp[i];
}
}
void mergeSort(student **S, int left, int right, student **tmp)
{
if(left<right){
int mid = (left+right)/2;
mergeSort(S, left, mid, tmp);
mergeSort(S, mid+1, right, tmp);
merge(S, left, mid, right, tmp);
}
}
int main(void)
{
int locations;
cin>>locations;
int K;
student **S = new student* [MAX];
int rear = -1;
for(int i=1; i<=locations; i++){
cin>>K;
for(int j=0; j<K; j++){
S[++rear] = new student;
cin>>S[rear]->id>>S[rear]->grade;
S[rear]->location = i;
S[rear]->localRank = 0;
}
}
student **tmp = new student* [MAX];
mergeSort(S, 0, rear, tmp);
int left=0, right=0;
for(int i=1; i<=rear; i++){
if(S[i]->grade==S[left]->grade){
right = i;
}else{
if(left<right){
quickSort(S, left, right);
}
left = i;
right = i;
}
}
if(left<right){
quickSort(S, left, right);
}
int localS[locations+1];
int localLast[locations+1];
for(int i=1; i<=locations; i++){
localS[i] = 0;
localLast[i] = -1;
}
int local, last;
for(int i=0; i<=rear; i++){
local = S[i]->location;
localS[local]++;
if(i>0){
if(S[i]->grade == S[i-1]->grade){
S[i]->Rank = S[i-1]->Rank;
}else{
S[i]->Rank = i+1;
}
last = localLast[local];
if(last != -1){
if(S[i]->grade == S[last]->grade){
S[i]->localRank = S[last]->localRank;
}else{
S[i]->localRank = localS[local];
localLast[local] = i;
}
}else{
localLast[local] = i;
S[i]->localRank = localS[local];
}
}else{
S[i]->Rank = i+1;
S[i]->localRank = i+1;
localLast[local] = i;
}
}
cout<<rear+1<<endl;
for(int i=0; i<=rear; i++){
cout<<S[i]->id<<" "<<S[i]->Rank<<" "<<S[i]->location<<" "<<S[i]->localRank<<endl;
}
for(int i=0; i<=rear; i++){
delete S[i];
}
delete [] S;
delete [] tmp;
return 0;
}