PAT 08-排序3. Talent and Virtue

来源:互联网 发布:淘宝客联盟 编辑:程序博客网 时间:2024/06/06 08:53

原题网址:http://www.patest.cn/contests/mooc-ds/08-%E6%8E%92%E5%BA%8F3

08-排序3. Talent and Virtue (25)

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 (<=105), the total number of people to be ranked; L (>=60), 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 (<100), 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 (<=N), 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 8010000001 64 9010000002 90 6010000011 85 8010000003 85 8010000004 80 8510000005 82 7710000006 83 7610000007 90 7810000008 75 7910000009 59 9010000010 88 4510000012 80 10010000013 90 9910000014 66 60
Sample Output:
1210000013 90 9910000012 80 10010000003 85 8010000011 85 8010000004 80 8510000007 90 7810000006 83 7610000005 82 7710000002 90 6010000014 66 6010000008 75 7910000001 64 90

思路:

本题的核心点在于不同优先级的排序,只需要对sort()函数的comp比较函数进行适当修改就可以达到目的。我本想在先把所有信息先输入装入一个vector容器后在进行分类,后来发现可以直接一输入就进行判断,分别放置在sages, noblemen, foolmen, rest四个vector容器里更加方便。
<span style="font-family:Courier New;">//PAT 08-3 #include<iostream>#include<algorithm>#include<vector>using namespace std;struct Inform{int ID_number;int Virtue_Grade;int Talent_Grade;int Total_Grade;     //the sum of Virtue_Grade and Talent_Grade };//define how the sort function define the orderbool comp(Inform x, Inform y);int main(){int N, lower_bound, higher_bound;cin >> N >> lower_bound >> higher_bound;Inform a;int i;vector<Inform> sages;vector<Inform> noblemen;vector<Inform> foolmen;vector<Inform> rest;vector<Inform>::iterator iter;vector<Inform>::size_type totalNum;for(i = 0; i < N; ++i){scanf("%d %d %d", &a.ID_number, &a.Virtue_Grade, &a.Talent_Grade);a.Total_Grade = a.Virtue_Grade + a.Talent_Grade;//classify the input informationif(a.Virtue_Grade >= higher_bound && a.Talent_Grade >= higher_bound)sages.push_back(a);else if(a.Virtue_Grade >= higher_bound && a.Talent_Grade >= lower_bound)noblemen.push_back(a);else if(a.Virtue_Grade >= lower_bound && a.Talent_Grade >= lower_bound)if(a.Virtue_Grade >= a.Talent_Grade)foolmen.push_back(a);elserest.push_back(a);elsetotalNum--;}//sort the four kind of people separatelysort(sages.begin(), sages.end(), comp);sort(noblemen.begin(), noblemen.end(), comp);sort(foolmen.begin(), foolmen.end(), comp);sort(rest.begin(), rest.end(), comp);totalNum = sages.size() + noblemen.size() + foolmen.size() + rest.size();//print the sorted ordercout << totalNum << '\n';for(iter = sages.begin(); iter != sages.end(); ++iter)printf("%d %d %d\n", iter->ID_number, iter->Virtue_Grade, iter->Talent_Grade);for(iter = noblemen.begin(); iter != noblemen.end(); ++iter)printf("%d %d %d\n", iter->ID_number, iter->Virtue_Grade, iter->Talent_Grade);for(iter = foolmen.begin(); iter != foolmen.end(); ++iter)printf("%d %d %d\n", iter->ID_number, iter->Virtue_Grade, iter->Talent_Grade);for(iter = rest.begin(); iter != rest.end(); ++iter)printf("%d %d %d\n", iter->ID_number, iter->Virtue_Grade, iter->Talent_Grade);return 0;}bool comp(Inform x, Inform y){if(x.Total_Grade > y.Total_Grade)//Total_Grade in non-increasing order return true;else if(x.Total_Grade == y.Total_Grade){if(x.Virtue_Grade > y.Virtue_Grade)//Virtue_Grade in non-increasing orderreturn true;else if(x.Virtue_Grade == y.Virtue_Grade)if(x.ID_number < y.ID_number)//ID_number in increasing orderreturn true;}return false;}</span>


提交评测结果:

测试点结果用时(ms)内存(kB)得分/满分0答案正确125612/121答案正确13642/22答案正确287523/33答案正确5734243/34答案正确5733603/35答案正确12362/2
0 0