About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣人)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(君子)"; being good in neither is a "fool man(愚人)"; yet a fool man is better than a "small man(小人)" who prefers talent than virtue.
Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang's theory.
Input Specification:
Each input file contains one test case. Each case first gives 3 positive integers in a line: N (≤), the total number of people to be ranked; L (≥), the lower bound of the qualified grades -- that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<), the higher line of qualification -- that is, those with both grades not below this line are considered as the "sages", and will be ranked in non-increasing order according to their total grades. Those with talent grades below H but virtue grades not are cosidered as the "noblemen", and are also ranked in non-increasing order according to their total grades, but they are listed after the "sages". Those with both grades below H, but with virtue not lower than talent are considered as the "fool men". They are ranked in the same way but after the "noblemen". The rest of people whose grades both pass the L line are ranked after the "fool men".
Then N lines follow, each gives the information of a person in the format:
ID_Number Virtue_Grade Talent_Grade
where ID_Number
is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space.
Output Specification:
The first line of output must give M (≤), the total number of people that are actually ranked. Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID's.
Sample Input:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
Sample Output:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
题意:
根据所给的规则排序。(认真读题)
思路:
如果在sort中重载比较函数,会有两组数据超时,但是如果在结构体中重载<则不会超时。
Code:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 struct People { 6 string id; 7 int virtue_grade; 8 int talent_grade; 9 int total_grade; 10 11 bool operator<(const People& b) { 12 if (total_grade != b.total_grade) return total_grade > b.total_grade; 13 if (virtue_grade != b.virtue_grade) 14 return virtue_grade > b.virtue_grade; 15 else 16 return id < b.id; 17 } 18 }; 19 20 int main() { 21 int n, l, h, total = 0; 22 cin >> n >> l >> h; 23 vector<People> sages, nobleman, foolman, smallman; 24 for (int i = 0; i < n; ++i) { 25 string id; 26 int virtue_grade, talent_grade, total_grade; 27 cin >> id >> virtue_grade >> talent_grade; 28 if (virtue_grade >= l and talent_grade >= l) { 29 total++; 30 total_grade = virtue_grade + talent_grade; 31 People temp{id, virtue_grade, talent_grade, total_grade}; 32 if (virtue_grade >= h && talent_grade >= h) 33 sages.push_back(temp); 34 else if (virtue_grade >= h) 35 nobleman.push_back(temp); 36 else if (virtue_grade >= talent_grade) 37 foolman.push_back(temp); 38 else 39 smallman.push_back(temp); 40 } 41 } 42 cout << total << endl; 43 sort(sages.begin(), sages.end()); 44 for (int i = 0; i < sages.size(); ++i) 45 cout << sages[i].id << " " << sages[i].virtue_grade << " " 46 << sages[i].talent_grade << endl; 47 sort(nobleman.begin(), nobleman.end()); 48 for (int i = 0; i < nobleman.size(); ++i) 49 cout << nobleman[i].id << " " << nobleman[i].virtue_grade << " " 50 << nobleman[i].talent_grade << endl; 51 sort(foolman.begin(), foolman.end()); 52 for (int i = 0; i < foolman.size(); ++i) 53 cout << foolman[i].id << " " << foolman[i].virtue_grade << " " 54 << foolman[i].talent_grade << endl; 55 sort(smallman.begin(), smallman.end()); 56 for (int i = 0; i < smallman.size(); ++i) 57 cout << smallman[i].id << " " << smallman[i].virtue_grade << " " 58 << smallman[i].talent_grade << endl; 59 60 return 0; 61 }