PAT_A 1062 Talent and Virtue & PAT_B 1015 德才论

PAT_A 1062 Talent and Virtue & PAT_B 1015 德才论

分析

这两道题除了题目使用的语言不同,其实是讲的内容一样的,因此放在一起了。由于做题时看到的是英文题面,所以分析中的描述可能与中文题面略有不同。

题目要求根据输入的人物的“道德值”(Virtue_Grade )与 “才能值”(Talent_Grade)按照指定规则进行排序并输出。一开始尝试用vector与直接插入排序,发现超出时间限制,因此改用了快排,由于本题的排序规则与输出规则均有特殊的规定,所以手搓的快排以及sort中的cmp有一些奇怪。题目的具体规则如下所述。

其指定的规则为:

首先给出及格值(L)、优秀值(H),若任一指标未及格则不参与排序。将参与排序的人分为圣人(sages),君子(noblemen),愚人(fool men),其他人(rest of people),

划分的规则为:

  • 只有参与排序的人才参与划分;

  • tag 0 “道德值”(Virtue_Grade )与 “才能值”(Talent_Grade)均优秀(v>=h, t>=h)的为圣人(sages);

  • tag 1 “道德值”(Virtue_Grade )优秀,但 “才能值”(Talent_Grade)不优秀仅及格(v>=h, h>t>=l)的为君子(noblemen);

  • tag 2 “道德值”(Virtue_Grade )与 “才能值”(Talent_Grade)均不优秀仅及格,且道德值大于等于才能值(h>v>=l, h>t>=l, v>=t)的为愚人(fool men);

  • tag 3 剩下的“道德值”(Virtue_Grade )与 “才能值”(Talent_Grade)均不优秀仅及格,且道德值小于才能值(h>v>=l, h>t>=l, v<t)的人为其他人(rest of people)。

输出的规则为:

  • 第一行输出参与排序的人数
  • 在参与排序的人中,按照圣人(sages),君子(noblemen),愚人(fool men),其他人(rest of people)的次序输出;
  • 在同种人之间,总分(total number)高的排序在前;
  • 总分相同时,道德值(Virtue_Grade)高的排序在前;
  • 道德值仍相同时,识别号(ID_Number)低的排序在前。

PAT_A 1062 Talent and Virtue

题目的描述

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\ (≤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 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 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

PAT_B 1015 德才论

题目的描述

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为: \(N\ (≤10^5)\),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

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
结尾无空行

输出样例:

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
结尾无空行

AC的代码

#include<bits/stdc++.h>

using namespace std;

class person{
    public:
    int ID,tag;
    int VG,TG,L,H,TOT;
    person(){}
    person(int  id, int vg, int tg,int l,int h){
        ID = id;
        VG = vg;
        TG = tg;
        L = l;
        H = h;
        TOT = vg + tg;
        if(tg>=h&&vg>=h){tag=0;}//sages
        else if(vg>=h){tag=1;}//nobelmen
        else if(vg<h&&vg>=tg){tag=2;}//fool men
        else if(vg<h&&vg<tg){tag=3;}//rest people
    }


    bool operator >(person t){
        if(this->tag==t.tag){
            return 
                this->TOT == t.TOT ? 
                (this->VG == t.VG ? this->ID < t.ID : this->VG > t.VG) 
                : this->TOT>t.TOT;
        }
        else{
            return this->tag<t.tag;
        }
    }

    bool operator <(person t){
        if(this->tag==t.tag){
            return 
                !(this->TOT == t.TOT ? 
                (this->VG == t.VG ? this->ID < t.ID : this->VG > t.VG) 
                : this->TOT>t.TOT);
        }
        else{
            return this->tag>t.tag;
        }
    }

};
bool cmp(person a,person t){
        if(a.tag==t.tag)
            return 
                a.TOT == t.TOT ? 
                (a.VG == t.VG ? a.ID < t.ID : a.VG > t.VG) 
                : a.TOT>t.TOT;
        else{
            return a.tag<t.tag;
        }
}
void my_sorted(array<person,100001> &array,int lefts, int right){
    if(right-lefts<2||lefts<0||right>100000)return;
    int l=lefts,r=right-1;
    person t = array[l];
    while(l<r){
        while(r>l&&array[r]<t)r--;
        if(r>l){array[l]=array[r];l++;}
        while(r>l&&array[l]>t)l++;
        if(r>l){array[r]=array[l];r--;}
    }
    array[l]=t;
    my_sorted(array,lefts,l);
    my_sorted(array,l+1,right);
}
int main(){
    int N,L,H,c=0;
    scanf("%d%d%d",&N,&L,&H);
    array<person,100001> ranks;
    while(N--){
        int id;
        int v,t;
        scanf("%d%d%d",&id,&v,&t);
        if(t<L||v<L) continue;
        person tp(id,v,t,L,H);
        ranks[c++]=tp;
        //直接插入排序
//         int i =c-1;
//         while(i>=1&&tp>ranks[i-1]){
//             ranks[i]=ranks[i-1];
//             i--;
//         }
//         ranks[i]=tp;

    }
    //快排 这里元素的大小关系要注意
//     sort(ranks.begin(),ranks.begin()+c,cmp);
    my_sorted(ranks,0,c);
    printf("%d\n",c);

    for(int i=0;i<c;i++){
        printf("%d %d %d\n",ranks[i].ID,ranks[i].VG,ranks[i].TG);
    }
    return 0;
}
posted @ 2022-01-24 22:58  ghosteq  阅读(32)  评论(0编辑  收藏  举报