【PAT甲级】1062 Talent and Virtue (25 分)

题意:

输入三个正整数N,L,H(N<=1E5,L>=60,H<100,H>L),分别代表人数,及格线和高水平线。接着输入N行数据,每行包括一个人的ID,道德数值和才能数值。一个人的道德和才能都不低于H时为圣人,道德不低于H才能不低于L时为贵族,道德和才能都不低于L且道德不低于才能时为愚者,道德和才能都不低于L且道德低于才能时为小人(圣人优先判断,圣人的才能可以高于道德)。分别在自己的所在的群体内排序,依照道德才能总和降序排序,第二优先为才能的数值降序,第三优先为id的升序。按照题意输出排序的总人数以及输出它们的信息。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef struct student{
 5     string id;
 6     int v,t;
 7     int sum;
 8 };
 9 student sage[100007],noble[100007],fool[100007],small[100007];
10 bool cmp(student a,student b){
11     if(a.sum!=b.sum)
12         return a.sum>b.sum;
13     if(a.v!=b.v)
14         return a.v>b.v;
15     return a.id<b.id;
16 }
17 int main(){
18     ios::sync_with_stdio(false);
19     cin.tie(NULL);
20     cout.tie(NULL);
21     int n,l,h;
22     cin>>n>>l>>h;
23     int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
24     for(int i=1;i<=n;++i){
25         string id;
26         cin>>id;
27         int v,t;
28         cin>>v>>t;
29         if(v>=h&&t>=h){
30             sage[++cnt1].id=id;
31             sage[cnt1].v=v;
32             sage[cnt1].t=t;
33             sage[cnt1].sum=v+t;
34         }
35         else if(v>=h&&t>=l){
36             noble[++cnt2].id=id;
37             noble[cnt2].v=v;
38             noble[cnt2].t=t;
39             noble[cnt2].sum=v+t;
40         }
41         else if(v>=l&&t>=l&&v>=t){
42             fool[++cnt3].id=id;
43             fool[cnt3].v=v;
44             fool[cnt3].t=t;
45             fool[cnt3].sum=v+t;
46         }
47         else if(v>=l&&t>=l&&v<t){
48             small[++cnt4].id=id;
49             small[cnt4].v=v;
50             small[cnt4].t=t;
51             small[cnt4].sum=v+t;
52         }
53     }
54     sort(sage+1,sage+1+cnt1,cmp);
55     sort(noble+1,noble+1+cnt2,cmp);
56     sort(fool+1,fool+1+cnt3,cmp);
57     sort(small+1,small+1+cnt4,cmp);
58     cout<<cnt1+cnt2+cnt3+cnt4<<"
";
59     for(int i=1;i<=cnt1;++i)
60         cout<<sage[i].id<<" "<<sage[i].v<<" "<<sage[i].t<<"
";
61     for(int i=1;i<=cnt2;++i)
62         cout<<noble[i].id<<" "<<noble[i].v<<" "<<noble[i].t<<"
";
63     for(int i=1;i<=cnt3;++i)
64         cout<<fool[i].id<<" "<<fool[i].v<<" "<<fool[i].t<<"
";
65     for(int i=1;i<=cnt4;++i)
66         cout<<small[i].id<<" "<<small[i].v<<" "<<small[i].t<<"
";
67     return 0;
68 }