PAT_A1062 / PAT_B1015 | Talent and Virtue

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(Saint)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(Gentleman"; being good in neither is a "fool man(Fool)"; yet a fool man is better than a "small man(Villain)" 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 (≤10​5​​), 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

First of all, sort out the classification relationship of people in the questions

As long as there is a virtual or talent lower than l, it will not be included, and the rest will be included as follows:

  • Saint: virtual > = h and talent > = h
  • Gentleman: virtual > = h and talent < H
  • Fool: virtual < h and talent < h and virtual > = talent
  • Villain: virtual < h and talent < h and virtual < talent

There are many elements of human beings, which are stored in structures, and classified according to saints, gentlemen, fools and villains, so you can create an array of structures

struct node {
    long long id;
    int virtue;
    int talent;
    int total;
} sage[MAX], nobleman[MAX], fool_man[MAX], small_man[MAX];

After judgment, the input data will be stored in the corresponding structure array in accordance with the standards, and updated to the number of elements of the structure array at the same time.

After all the input is stored, sort the array of each structure in turn. You can directly use the sort function in the c++"algorithm" library. See this article for details on how to use it: Sorting algorithm | use of sort function

 

This question requires sorting in descending order according to the total score. When the total score is the same, it is sorted in descending order according to the score of virtual. If it is the same, it is sorted in ascending order according to the id. The cmp function should read as follows:

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;
}

 

The complete code is as follows:

//
// 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)  //Not included in the ranking
            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 

Welcome to pay attention to the personal public number "chicken wings programming". Here is a serious and clever code farmer.

----Be the best blogger and the most solid programmer----

The purpose is to write every article carefully, and usually summarize the notes into push updates~

 

82 original articles published, 36 praised, 10000 visitors+
Private letter follow

Tags: REST less Programming

Posted on Tue, 11 Feb 2020 11:16:13 -0500 by DynV