1062. 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 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


题目要求按照sage、nobleman、foolman、smallman四个等级分榜排序,需要注意的是不能用一个vector根据等级>总分>道德>ID的顺序排序,因为题目要求后面的等级所有人都要在前面等级的后面,如果用一个vector从头排到尾,会出现等级交叉的情况。

注意这类分榜排序问题最好给每个榜一个vector,这道题目解决起来比较简单,设计一个结构体,把所有数据用int存储,在输入时过滤掉分数不达L线的人,分别压入4个vector,然后重载<按照题目要求排序。

为了提高效率,id用int存储,输出时用%08d保证前导0的个数。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// 输入:人数N 底线L(talent、virtue都高于底线才排) 高线H(T V都高于为圣人(按总分排序)
// V >= H 但 T < H 为贵族(按照总分排序) > 都低于H但是V更高为愚人(按照总分排序)> 余下的均高于L的人
// 总分一样按照V,再一样按照ID升序

int N,L,H;

struct Person{
    int id;
    int virtue;
    int talent;
    int total;

    bool operator < (const Person other) const{
            if(total < other.total){
                return false;
            }else if(total > other.total){
                return true;
            }else{
                if(virtue < other.virtue){
                    return false;
                }else if(virtue > other.virtue){
                    return true;
                }else{
                    return id < other.id;
                }
            }
        }
};

int main()
{
    cin >> N >> L >> H;
    vector<vector<Person> > persons(4);
    int id,virtue,talent;
    int rankCnt = 0;
    for(int i = 0; i < N; i++){
        scanf("%d%d%d",&id,&virtue,&talent);
        if(virtue < L || talent < L) continue;
        rankCnt++;
        Person p = Person();
        p.id = id;
        p.virtue = virtue;
        p.talent = talent;
        p.total = virtue + talent;
        if(virtue >= H && talent >= H){
            persons[0].push_back(p);
        }
        else if(virtue >= H && talent < H){
            persons[1].push_back(p);
        }
        else if(virtue < H && talent < H && virtue >= talent){
            persons[2].push_back(p);
        }
        else{
            persons[3].push_back(p);
        }
    }
    cout << rankCnt << endl;
    for(int i = 0; i < persons.size(); i++){
        sort(persons[i].begin(),persons[i].end());
        for(int j = 0; j < persons[i].size(); j++){
            printf("%08d %d %d
",persons[i][j].id,persons[i][j].virtue,persons[i][j].talent);
        }
    }

    return 0;
}


原文地址:https://www.cnblogs.com/aiwz/p/6154098.html