1025 PAT Ranking (25 分)
传送门
#include <bits/stdc++.h>
using namespace std;
struct node {
string id;
int grade;
int local_rank;
int final_rank;
int local_number;
} ;
bool cmp ( node a, node b) {
if ( a. grade!= b. grade)
return a. grade > b. grade;
else
return a. id< b. id;
}
int main ( ) {
int n, k, m= 0 ;
cin>> n;
vector< node> list;
for ( int j = 0 ; j < n; j++ ) {
cin>> k;
struct node a;
vector< node> temp;
for ( int i = 0 ; i < k; i++ ) {
cin>> a. id>> a. grade;
m++ ;
a. local_number = j+ 1 ;
temp. push_back ( a) ;
}
sort ( temp. begin ( ) , temp. end ( ) , cmp) ;
temp[ 0 ] . local_rank = 1 ;
int t = 1 ;
list. push_back ( temp[ 0 ] ) ;
for ( int i = 1 ; i < k; i++ ) {
if ( temp[ i] . grade== temp[ i- 1 ] . grade) {
temp[ i] . local_rank = temp[ i- 1 ] . local_rank;
t++ ;
} else {
t++ ;
temp[ i] . local_rank = t;
}
list. push_back ( temp[ i] ) ;
}
}
sort ( list. begin ( ) , list. end ( ) , cmp) ;
int t = 1 ;
list[ 0 ] . final_rank = 1 ;
printf ( "%d\n" , m) ;
for ( int i = 1 ; i < m; i++ ) {
if ( list[ i] . grade== list[ i- 1 ] . grade) {
list[ i] . final_rank = list[ i- 1 ] . final_rank;
t++ ;
} else {
t++ ;
list[ i] . final_rank = t;
}
}
for ( auto it : list) {
cout<< it. id<< " " << it. final_rank<< " " << it. local_number<< " " << it. local_rank<< endl;
}
return 0 ;
}