PAT grade B 1015 theory of virtue and talent (25 points)

Posted by Ilovetopk on Sun, 23 Jan 2022 07:36:37 +0100

Sima Guang, a historian of the Song Dynasty, wrote a famous "theory of virtue and talent" in Zizhi Tongjian: "therefore, the perfection of talent and morality is called a saint, the death of talent and morality is called a fool, the victory of virtue is called a gentleman, and the victory of virtue is called a villain. If a gentleman takes the skill of man, he can't be a saint. If a gentleman takes it, he won't be a fool rather than a villain."

The scores of virtue and talent of a group of candidates are given. Please give the admission ranking according to Sima Guang's theory.

Input format:

Input the first line and give 3 positive integers, respectively: N (≤ 105), that is, the total number of candidates; L (≥ 60), which is the lowest score line for admission, that is, candidates whose moral score and talent score are not lower than l are eligible to be considered for admission; H (< 100) is the priority admission line - those whose moral score and talent score are not lower than this line are defined as "full of talent and virtue", and such candidates are ranked from high to low according to the total score of morality and talent; The candidates who can't get the talent score but get the moral score line belong to "virtue wins talent", which is also sorted according to the total score, but they are ranked behind the first category of candidates; Candidates whose moral and talent scores are lower than h, but whose moral scores are not lower than talent scores belong to "both talent and morality" but still have "virtue and talent", which are sorted according to the total score, but ranked behind the second category of candidates; Other candidates who reach the lowest line L are also ranked according to the total score, but they are behind the third category of candidates.

Then, in line N, each line gives the information of one candidate, including: admission card number, German score, in which the admission card number is an 8-digit integer, and German score is an integer in the interval [0, 100]. Numbers are separated by spaces.

Output format:

The first line of output first gives the number of candidates reaching the lowest score line M, and then M lines. Each line outputs the information of one candidate according to the input format, and the candidates are sorted from high to low according to the rules described in the input. When there are many candidates with the same total score, they are arranged in descending order according to their moral score; If the scores are also in parallel, they will be output in ascending order of the admission number.

Input example:

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

Output example:

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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
struct people {
    int id;
    int t_grade;
    int v_grade;
    int sum;
};

bool cmp(people p1, people p2) {
    if (p1.sum != p2.sum)
        return p1.sum > p2.sum;
    else {
        if (p1.v_grade != p2.v_grade)
            return p1.v_grade > p2.v_grade;
        else
            return p1.id < p2.id;
    }
}

int main() {
    int N, low, high;
    iostream::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> low >> high;
    people p;
    vector<people> sage;
    vector<people> nobleman;
    vector<people> fool;
    vector<people> rest;
    for (int i = 0; i < N; ++i) {
        cin >> p.id >> p.v_grade >> p.t_grade;
        p.sum = p.v_grade + p.t_grade;
        if (p.t_grade >= high and p.v_grade >= high)
            sage.push_back(p);
        else if (p.v_grade >= high and p.t_grade >= low)
            nobleman.push_back(p);
        else if (p.t_grade <= p.v_grade and p.t_grade >= low and p.v_grade >= low)
            fool.push_back(p);
        else if (p.t_grade >= low and p.v_grade >= low)
            rest.push_back(p);
    }
    sort(sage.begin(), sage.end(), cmp);
    sort(nobleman.begin(), nobleman.end(), cmp);
    sort(fool.begin(), fool.end(), cmp);
    sort(rest.begin(), rest.end(), cmp);
    cout << sage.size() + nobleman.size() + fool.size() + rest.size() << endl;
    for (auto c: sage)
        cout << c.id << " " << c.v_grade << " " << c.t_grade << endl;
    for (auto c: nobleman)
        cout << c.id << " " << c.v_grade << " " << c.t_grade << endl;
    for (auto c: fool)
        cout << c.id << " " << c.v_grade << " " << c.t_grade << endl;
    for (auto c: rest)
        cout << c.id << " " << c.v_grade << " " << c.t_grade << endl;
    return 0;
}

Reduced version code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
struct people {
    int id;
    int t_grade;
    int v_grade;
    int sum;
    int level;
};

bool cmp(people p1, people p2) {
    if (p1.level != p2.level)
        return p1.level < p2.level;
    else {
        if (p1.sum != p2.sum)
            return p1.sum > p2.sum;
        else {
            if (p1.v_grade != p2.v_grade)
                return p1.v_grade > p2.v_grade;
            else
                return p1.id < p2.id;
        }
    }
}

int main() {
    int N, low, high;
    iostream::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> low >> high;
    people p;
    vector<people> peo;
    for (int i = 0; i < N; ++i) {
        cin >> p.id >> p.v_grade >> p.t_grade;
        p.sum = p.v_grade + p.t_grade;
        if (p.t_grade < low or p.v_grade < low)
            continue;
        if (p.t_grade >= high and p.v_grade >= high)
            p.level = 1;
        else if (p.v_grade >= high and p.t_grade >= low)
            p.level = 2;
        else if (p.t_grade <= p.v_grade and p.t_grade >= low and p.v_grade >= low)
            p.level = 3;
        else if (p.t_grade >= low and p.v_grade >= low)
            p.level = 4;
        peo.push_back(p);
    }
    sort(peo.begin(), peo.end(), cmp);
    cout << peo.size() << endl;
    for (auto c: peo)
        cout << c.id << " " << c.v_grade << " " << c.t_grade << endl;
    return 0;
}