• PAT甲级——A1055 The World's Richest


    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the net worths of N people, you must find the M richest people in a given range of their ages.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤) - the total number of people, and K (≤) - the number of queries. Then N lines follow, each contains the name (string of no more than 8 characters without space), age (integer in (0, 200]), and the net worth (integer in [−]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤) - the maximum number of outputs, and [AminAmax] which are the range of ages. All the numbers in a line are separated by a space.

    Output Specification:

    For each query, first print in a line Case #X: where X is the query number starting from 1. Then output the M richest people with their ages in the range [AminAmax]. Each person's information occupies a line, in the format

    Name Age Net_Worth
    

    The outputs must be in non-increasing order of the net worths. In case there are equal worths, it must be in non-decreasing order of the ages. If both worths and ages are the same, then the output must be in non-decreasing alphabetical order of the names. It is guaranteed that there is no two persons share all the same of the three pieces of information. In case no one is found, output None.

    Sample Input:

    12 4
    Zoe_Bill 35 2333
    Bob_Volk 24 5888
    Anny_Cin 95 999999
    Williams 30 -22
    Cindy 76 76000
    Alice 18 88888
    Joe_Mike 32 3222
    Michael 5 300000
    Rosemary 40 5888
    Dobby 24 5888
    Billy 24 5888
    Nobody 5 0
    4 15 45
    4 30 35
    4 5 95
    1 45 50
    

    Sample Output:

    Case #1:
    Alice 18 88888
    Billy 24 5888
    Bob_Volk 24 5888
    Dobby 24 5888
    Case #2:
    Joe_Mike 32 3222
    Zoe_Bill 35 2333
    Williams 30 -22
    Case #3:
    Anny_Cin 95 999999
    Michael 5 300000
    Alice 18 88888
    Cindy 76 76000
    Case #4:
    None

    第一个代码是用vector存储,但遍历超时,第二个时使用数组存储,测试通过.
    搞不明白同样的算法复杂度,为什么vector遍历时间就比数组差那么多?
     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 #include <algorithm>
     5 using namespace std;
     6 struct Node
     7 {
     8     string name;
     9     int age, val;
    10 }node;
    11 int N, K, M, num, Amin, Amax;
    12 bool cmp(Node a, Node b)
    13 {
    14     if (a.val == b.val && a.age == b.age)
    15         return a.name < b.name;
    16     else if (a.val == b.val)
    17         return a.age < b.age;
    18     else
    19         return a.val > b.val;
    20 }
    21 int main()
    22 {
    23     cin >> N >> K;
    24     vector<Node>v;
    25     vector<vector<Node>>res(K);
    26     for (int i = 0; i < N; ++i)
    27     {
    28         cin >> node.name >> node.age >> node.val;
    29         v.push_back(node);
    30     }
    31     sort(v.begin(), v.end(), cmp);
    32     for (int i = 0; i < K; ++i)
    33     {
    34         cin >> num >> Amin >> Amax;
    35         int flag = 0;
    36         cout << "Case #" << i + 1 << ":" << endl;
    37         for (auto a : v)
    38         {
    39             if (a.age >= Amin && a.age <= Amax)
    40             {
    41                 cout << a.name << " " << a.age << " " << a.val << endl;
    42                 flag++;
    43                 if (flag == num)
    44                     break;
    45             }
    46         }
    47         if (flag == 0)
    48             cout << "None" << endl;
    49     }
    50     return 0;
    51 }
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct Person{//存储相应信息的结构体
     4     string name;
     5     int age,worth;
     6 };
     7 Person person[(int)(1e5+5)];
     8 int main(){
     9     int N,K;
    10     scanf("%d%d",&N,&K);
    11     for(int i=0;i<N;++i)
    12         cin>>person[i].name>>person[i].age>>person[i].worth;
    13     sort(person,person+N,[](const Person&p1,const Person&p2){//比较函数
    14         if(p1.worth!=p2.worth)
    15             return p1.worth>p2.worth;
    16         else if(p1.age!=p2.age)
    17             return p1.age<p2.age;
    18         else
    19             return p1.name<p2.name;
    20     });//排序
    21     for(int i=1;i<=K;++i){
    22         int M,Amin,Amax;
    23         bool f=false;
    24         scanf("%d%d%d",&M,&Amin,&Amax);
    25         printf("Case #%d:
    ",i);
    26         for(int j=0;j<N&&M>0;++j)//遍历数组
    27             if(person[j].age>=Amin&&person[j].age<=Amax){//找到符合要求的人
    28                 printf("%s %d %d
    ",person[j].name.c_str(),person[j].age,person[j].worth);//输出
    29                 --M;//将M递减
    30                 f=true;//表示已输出过
    31             }
    32         if(!f)//在该年龄段没有任何输出
    33             printf("None
    ");//输出None
    34     }
    35     return 0;
    36 }



  • 相关阅读:
    [转帖]能感动天地的老人,你拿什么来感动CCTV
    SaaS, 8,9点的太阳
    ERP软件开源是中国软件行业未来之路
    觉得为时已晚的时候,恰恰是最早的时候。
    新画皮故事——ERP软件为什么要免费
    如何定制SharePoint“欢迎”菜单?
    软件产品在什么情况下一定要走精品路线
    我的blogs
    测试环境中安装sharepoint server 2010过程中出现的一些问题及解决过程
    windows server 2008 与windows server 2008 r2区别
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11285996.html
Copyright © 2020-2023  润新知