【PAT甲级】1055 The World's Richest (25 分)

题意:

输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富。K次询问,每次输入三个正整数M,L,R(M<=100,L,R<=200),输出至多M位年龄区间在L,R之间的老板的名字年龄和财富,按照财富降序,年龄升序,姓名字典序输出。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 string s;
 5 typedef struct account{
 6     string name;
 7     int age,worth;
 8 };
 9 bool cmp(account a,account b){
10     if(a.worth!=b.worth)
11         return a.worth>b.worth;
12     if(a.age!=b.age)
13         return a.age<b.age;
14     return a.name<b.name;
15 }
16 vector<account>vv;
17 vector<account>v;
18 int sum[207];
19 int main(){
20     ios::sync_with_stdio(false);
21     cin.tie(NULL);
22     cout.tie(NULL);
23     int n,k;
24     cin>>n>>k;
25     int age,w;
26     for(int i=1;i<=n;++i){
27         cin>>s>>age>>w;
28         account tamp;
29         tamp.name=s;
30         tamp.age=age;
31         tamp.worth=w;
32         vv.push_back(tamp);
33     }
34     int x,l,r;
35     sort(vv.begin(),vv.end(),cmp);
36     for(int i=0;i<vv.size();++i)//根据数据规模优化,大幅缩短运行时间
37         if(sum[vv[i].age]<100){
38             v.push_back(vv[i]);
39             ++sum[vv[i].age];
40         }
41     for(int i=1;i<=k;++i){
42         cin>>x>>l>>r;
43         cout<<"Case #"<<i<<":
";
44         int flag=0;
45         for(int j=0;j<v.size();++j){
46             if(v[j].age>=l&&v[j].age<=r){
47                 cout<<v[j].name<<" "<<v[j].age<<" "<<v[j].worth<<"
";
48                 --x;
49                 flag=1;
50             }
51             if(!x)
52                 break;
53         }
54         if(!flag)
55             cout<<"None
";
56     }
57     return 0;
58 }
保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
原文地址:https://www.cnblogs.com/ldudxy/p/11639172.html