1.只需要对要求排名的M个国家进行排名,其他国家直接舍弃
2.注意一下排名的方式:1,2,2,4而不是1,2,2,3
3.在给定要排序的国家号的时候,并不一定会按照从小到大的顺序给定,题目中也没有这种说明,所以最后输出的时候一定注意顺序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Country {
public :
int number, rank[ 4 ] , index;
double data[ 4 ] , people;
void print ( ) {
int min= rank[ 0 ] , minindex= 0 ;
for ( int i= 1 ; i< 4 ; i++ ) {
if ( rank[ i] < min) {
min= rank[ i] ;
minindex= i;
}
}
cout<< min<< ":" << minindex+ 1 << endl;
}
} ;
bool goldCom ( Country& a, Country& b) {
return a. data[ 0 ] > b. data[ 0 ] ;
}
bool medalCom ( Country& a, Country& b) {
return a. data[ 1 ] > b. data[ 1 ] ;
}
bool goldPeopleCom ( Country& a, Country& b) {
return a. data[ 2 ] > b. data[ 2 ] ;
}
bool medalPeopleCom ( Country& a, Country& b) {
return a. data[ 3 ] > b. data[ 3 ] ;
}
bool indexCom ( Country& a, Country& b) {
return a. index< b. index;
}
int main ( ) {
int N, M;
while ( cin>> N>> M) {
vector< Country> country;
vector< Country> co;
for ( int i= 0 ; i< N; i++ ) {
Country t;
cin>> t. data[ 0 ] >> t. data[ 1 ] >> t. people;
t. number= i;
country. push_back ( t) ;
}
for ( int i= 0 ; i< M; i++ ) {
int coNum;
cin>> coNum;
for ( vector< Country> :: iterator it= country. begin ( ) ; it!= country. end ( ) ; it++ ) {
if ( it- > number== coNum) {
it- > data[ 2 ] = it- > data[ 0 ] / it- > people;
it- > data[ 3 ] = it- > data[ 1 ] / it- > people;
it- > index= i;
co. push_back ( * it) ;
break ;
}
}
}
for ( int i= 0 ; i< 4 ; i++ ) {
switch ( i) {
case 0 :
sort ( co. begin ( ) , co. end ( ) , goldCom) ;
break ;
case 1 :
sort ( co. begin ( ) , co. end ( ) , medalCom) ;
break ;
case 2 :
sort ( co. begin ( ) , co. end ( ) , goldPeopleCom) ;
break ;
case 3 :
sort ( co. begin ( ) , co. end ( ) , medalPeopleCom) ;
break ;
}
int _rank= 0 , rank1= 1 ;
double _num= - 1 ;
for ( vector< Country> :: iterator it= co. begin ( ) ; it!= co. end ( ) ; it++ ) {
if ( _num!= it- > data[ i] ) {
it- > rank[ i] = rank1;
_rank= rank1;
_num= it- > data[ i] ;
} else
it- > rank[ i] = _rank;
rank1++ ;
}
}
sort ( co. begin ( ) , co. end ( ) , indexCom) ;
for ( vector< Country> :: iterator it= co. begin ( ) ; it!= co. end ( ) ; it++ ) {
it- > print ( ) ;
}
cout<< endl;
}
return 0 ;
}