1062 Talent and Virtue (25point(s))
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 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
首先梳理一下题中的人分类关系....
只要有 virtue 或 talent 低于 l 就不计入,其余的要如下要求计入:
- 圣人:virtue >= h 且 talent >= h
- 君子:virtue >= h 且 talent < h
- 愚人:virtue < h 且 talent < h 且 virtue >= talent
- 小人:virtue < h 且 talent < h 且 virtue < talent
人的要素比较多,使用结构体储存,而又要按照圣人、君子、愚人、小人分类,所以可以各创建一个结构体数组
struct node {
long long id;
int virtue;
int talent;
int total;
} sage[MAX], nobleman[MAX], fool_man[MAX], small_man[MAX];
输入的数据经过判断,将符合标准的依次存入对应的结构体数组内,并同时更新给个结构体数组的元素个数。
当输入全部储存完后,对各个结构体数组依次排序,可以直接使用c++"algorithm"库中的sort函数,具体如何使用可以看看这篇文章:排序算法 | sort函数的使用
本题要求按照总分降序排序,当总分一样按照virtue的分数降序排序,如果还一样,就按照id升序排序。cmp函数应该这样写:
bool cmp(struct node x, struct node y) {
if (x.total != y.total)
return x.total > y.total;
else if (x.virtue != y.virtue)
return x.virtue > y.virtue;
else
return x.id < y.id;
}
完整代码如下:
//
// Created by LittleCat on 2020/2/11.
//
#include <cstdio>
#include <algorithm>
#define MAX 100050
using namespace std;
struct node {
long long id;
int virtue;
int talent;
int total;
} sage[MAX], nobleman[MAX], fool_man[MAX], small_man[MAX];
bool cmp(struct node x, struct node y) {
if (x.total != y.total)
return x.total > y.total;
else if (x.virtue != y.virtue)
return x.virtue > y.virtue;
else
return x.id < y.id;
}
int main() {
int n, l, h;
scanf("%d %d %d", &n, &l, &h);
int count = 0, num_sage = 0, num_nobleman = 0, num_fool_man = 0, num_small_man = 0;
for (int i = 0; i < n; i++) {
long long id;
int v, t;
scanf("%lld %d %d", &id, &v, &t);
if (v < l || t < l) //不列入排名的情况
continue;
if (v >= h && t >= h) {
sage[num_sage].id = id;
sage[num_sage].virtue = v;
sage[num_sage].talent = t;
sage[num_sage++].total = t + v;
count++;
} else if (v >= h && t < h) {
nobleman[num_nobleman].id = id;
nobleman[num_nobleman].virtue = v;
nobleman[num_nobleman].talent = t;
nobleman[num_nobleman++].total = v + t;
count++;
} else if (v < h && t < h && v >= t) {
fool_man[num_fool_man].id = id;
fool_man[num_fool_man].virtue = v;
fool_man[num_fool_man].talent = t;
fool_man[num_fool_man++].total = v + t;
count++;
} else {
small_man[num_small_man].id = id;
small_man[num_small_man].virtue = v;
small_man[num_small_man].talent = t;
small_man[num_small_man++].total = v + t;
count++;
}
}
sort(sage, sage + num_sage, cmp);
sort(nobleman, nobleman + num_nobleman, cmp);
sort(fool_man, fool_man + num_fool_man, cmp);
sort(small_man, small_man + num_small_man, cmp);
printf("%d\n", count);
for(int i = 0; i < num_sage; i++)
printf("%lld %d %d\n", sage[i].id, sage[i].virtue, sage[i].talent);
for(int i = 0; i < num_nobleman; i++)
printf("%lld %d %d\n", nobleman[i].id, nobleman[i].virtue, nobleman[i].talent);
for(int i = 0; i < num_fool_man; i++)
printf("%lld %d %d\n", fool_man[i].id, fool_man[i].virtue, fool_man[i].talent);
for(int i = 0; i < num_small_man; i++)
printf("%lld %d %d\n", small_man[i].id, small_man[i].virtue, small_man[i].talent);
}
end
欢迎关注个人公众号“ 鸡翅编程 ”,这里是认真且乖巧的码农一枚。
---- 做最乖巧的博客er,做最扎实的程序员 ----
旨在用心写好每一篇文章,平常会把笔记汇总成推送更新~