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: , the total number of people to be ranked; , 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 , 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 considered 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 , 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
Ω
除了排序规则有点复杂外,也只是一道简单的信息分类输出题。根据司马光先生(砸缸的那位
)的理论,人分为圣人( )、君子( )、愚人( )、小人( )。给出人数N、H、L,以及每人的T、V,请按照前述的顺序分类并输出,同一类中按照 降序排列,相等时按照 降序排序,再相等时按照ID升序排列。
对于排序规则,只需要写一个cmp函数然后sort即可,其余就是如何组织数据结构的问题了。一开始我用map存储了ID所对应的T、V,然后分类时只存储ID,将map作为全局变量从而实现对ID的sort。不幸的是,提交后出现了超时,其实仔细想想,排序时需要不断地访问map,最后输出还要再访问一遍。而 ,如果说所有人都在同一类,那么访问map的时间将会是致命的。最后我采用了tuple,将ID、T、V都存储在一起(类似于结构体),消除了访问时间,Run Time陡跌。
🐎
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef tuple<string, int, int> tsii;
bool cmp(tsii &a, tsii &b)
{
if (get<1>(a) + get<2>(a) != get<1>(b) + get<2>(b))
return get<1>(a) + get<2>(a) > get<1>(b) + get<2>(b);
else if (get<1>(a) != get<1>(b))
return get<1>(a) > get<1>(b);
else
return get<0>(a) < get<0>(b);
}
int main()
{
int N, L, H, t, v;
scanf("%d %d %d", &N, &L, &H);
int valid = N;
vector<tuple<string, int, int>> men[4];
string s;
s.resize(8);
for (int i = 0; i < N; ++i)
{
scanf("%s %d %d", &s[0], &v, &t);
if (t < L || v < L)
{
valid -= 1;
continue;
}
else if (t >= H && v >= H)
men[0].emplace_back(s, v, t);
else if (t < H && v >= H)
men[1].emplace_back(s, v, t);
else if (t <= v && v < H)
men[2].emplace_back(s, v, t);
else
men[3].emplace_back(s, v, t);
}
printf("%d\n", valid);
for (auto &m: men)
{
sort(m.begin(), m.end(), cmp);
for (auto &k: m)
printf("%s %d %d\n", get<0>(k).c_str(), get<1>(k), get<2>(k));
}
}