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 85Sample 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
思路:注意排名方式,还有题目最后一句话,排名相同,按照注册号递增排序。。。。
#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> using namespace std; struct testee { string registration_number; int location_number; int score; testee(string num,int l_num,int s):registration_number(num),location_number(l_num),score(s){} bool operator>(struct testee b) { return score > b.score; } }; bool cmp(struct testee a,struct testee b) { if(a.score != b.score) return a.score > b.score; else return a.registration_number < b.registration_number; } int main() { int n; cin>>n; vector<testee> Testee; map<string,int> local_rank; map<string,int> final_rank; int sum = 0; int now_score = -1; int rank = 0; for(int i = 0; i < n; i++) { int K; cin>>K; vector<testee> tmp; for(int j = 0; j < K; j++) { string num; int l_num,s; cin>>num>>s; Testee.push_back(testee(num,i+1,s)); tmp.push_back(testee(num,i+1,s)); } sort(tmp.begin(),tmp.end(),cmp); now_score = -1; rank = 0; for(int t = 0; t < tmp.size(); t++) { if(tmp[t].score != now_score) { local_rank[tmp[t].registration_number] = t + 1; now_score = tmp[t].score; rank = t + 1; } else local_rank[tmp[t].registration_number] = rank; } } sort(Testee.begin(),Testee.end(),cmp); now_score = -1; rank = 0; for(int i = 0; i < Testee.size(); i++) { if(Testee[i].score != now_score) { final_rank[Testee[i].registration_number] = i + 1; now_score = Testee[i].score; rank = i + 1; } else final_rank[Testee[i].registration_number] = rank; } cout<<Testee.size()<<endl; for(int i = 0; i < Testee.size(); i++) { cout<<Testee[i].registration_number<<" "<<final_rank[Testee[i].registration_number]<<" "<<Testee[i].location_number<<" " <<local_rank[Testee[i].registration_number]<<endl; } return 0; }