PAT 1025. PAT Ranking (25)

1025. PAT Ranking (25)

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

  1 #include <iostream>
  2 #include <vector>
  3 #include <string>
  4 #include <algorithm>
  5 
  6 using namespace std;
  7 
  8 class Student
  9 {
 10 private:
 11     string registerNum;
 12     int score;
 13     int finalRank;
 14     int localRank;
 15     int locationNum;
 16 public:
 17     Student(){};
 18     ~Student(){};
 19     friend bool cmp(const Student& s1, const Student& s2);
 20     friend bool OutCmp(const Student& s1, const Student& s2);
 21     friend istream& operator>>(istream& is, Student& stu);
 22     friend ostream& operator<<(ostream& os, const Student& stu);
 23     void SetLocalRank(int rank){ localRank = rank; }
 24     void SetLocationNum(int num){ locationNum = num; }
 25     void SetFinalRank(int rank){ finalRank = rank; }
 26     int GetScore(){ return score; }
 27 };
 28 
 29 //the compare of the score
 30 bool cmp(const Student& s1, const Student& s2)
 31 {
 32     return s1.score > s2.score;
 33 }
 34 
 35 //the compare of the output
 36 bool OutCmp(const Student& s1, const Student& s2)
 37 {
 38     if (s1.finalRank == s2.finalRank)
 39         return s1.registerNum < s2.registerNum;
 40     else
 41         return s1.finalRank < s2.finalRank;
 42 }
 43 
 44 //input the testee information
 45 istream& operator>>(istream& is, Student& stu)
 46 {
 47     is >> stu.registerNum >> stu.score;
 48 
 49     return is;
 50 }
 51 
 52 //output the testee information
 53 ostream& operator<<(ostream& os, const Student& stu)
 54 {
 55     os << stu.registerNum << " " << stu.finalRank << " " << stu.locationNum << " " << stu.localRank;
 56 
 57     return os;
 58 }
 59 
 60 int main()
 61 {
 62     int locationNum;
 63     cin >> locationNum;
 64     vector<Student> final;
 65 
 66     for (int i = 1; i <= locationNum; i++)
 67     {
 68         int StuNum;
 69         cin >> StuNum;
 70         vector<Student> local;
 71 
 72         for (int j = 0; j < StuNum; j++)
 73         {
 74             Student stu;
 75             cin >> stu;
 76             stu.SetLocationNum(i);
 77             local.push_back(stu);
 78         }
 79         //set the rank of single local room rank
 80         sort(local.begin(), local.end(), cmp);
 81         int score = 101;
 82         int rank = 0;
 83         for (int j = 0; j < local.size(); j++)
 84         {
 85             if (local[j].GetScore() < score)
 86             {
 87                 local[j].SetLocalRank(j + 1);
 88                 score = local[j].GetScore();
 89                 rank = j + 1;
 90             }
 91             else 
 92                 local[j].SetLocalRank(rank);
 93         }
 94         final.insert(final.begin(), local.begin(), local.end());
 95     }
 96     //set the final rank
 97     sort(final.begin(), final.end(), cmp);
 98     int score = 101;
 99     int rank = 0;
100     for (int i = 0; i < final.size(); i++)
101     {
102         if (final[i].GetScore() < score)
103         {
104             final[i].SetFinalRank(i + 1);
105             score = final[i].GetScore();
106             rank = i + 1;
107         }
108         else
109             final[i].SetFinalRank(rank);
110 
111     }
112     sort(final.begin(), final.end(), OutCmp);
113     cout << final.size() << endl;
114     for (int i = 0; i < final.size(); i++)
115     {
116         cout << final[i];
117         if (i < final.size() - 1)
118             cout << endl;
119     }
120 }

 

posted @ 2015-08-07 13:14  JackWang822  阅读(155)  评论(0编辑  收藏  举报