zoukankan      html  css  js  c++  java
  • 1055 The World's Richest (25分)(水排序)

    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

    题目分析:写之前认为暴力排序再遍历选取满足条件的会超时 就用了认为稍微好一点的做法 。。但第三个测试点超时了 果然还是不行
    但学到了用upper_bound和lower_bound排序

    第三个点超时的
     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include <climits>
     3 #include<iostream>
     4 #include<vector>
     5 #include<queue>
     6 #include<map>
     7 #include<set>
     8 #include<stack>
     9 #include<algorithm>
    10 #include<string>
    11 #include<cmath>
    12 using namespace std;
    13 struct Person{
    14     string Name;
    15     int Age;
    16     int Net_Worth;
    17     Person(){}
    18     Person(string name,int a,int b):Name(name),Age(a),Net_Worth(b){}
    19     bool operator<(const Person p)const {
    20         return Age < p.Age;
    21     }
    22 };
    23 bool compare(const Person& a, const Person& b)
    24 {
    25     return (a.Net_Worth != b.Net_Worth) ? a.Net_Worth > b.Net_Worth :
    26         (a.Age != b.Age) ? a.Age < b.Age : a.Name < b.Name;
    27 }
    28 bool com(const Person& a, const Person& b)
    29 {
    30     return a.Age < b.Age;
    31 }
    32 int main()
    33 {
    34     int N, K;
    35     cin >> N >> K;
    36     vector<Person> V(N);
    37     for (int i = 0; i < N; i++)
    38         cin >> V[i].Name >> V[i].Age >> V[i].Net_Worth;
    39     sort(V.begin(), V.end(), com);
    40     for (int i = 1; i <= K; i++)
    41     {
    42         int max_count, Amin, Amax;
    43         int begin, end;
    44         cin >> max_count >>Amin >> Amax;
    45         begin = lower_bound(V.begin(), V.end(),Person("0",Amin,0)) - V.begin();
    46         end = upper_bound(V.begin(), V.end(),Person("0",Amax,0)) - V.begin();
    47         vector<Person> Ans;
    48         Ans.insert(Ans.begin(), V.begin() + begin, V.begin() + end);
    49         sort(Ans.begin(), Ans.end(), compare);
    50         printf("Case #%d:
    ", i);
    51         if (!Ans.size())
    52             cout << "None"<<endl;
    53         else
    54         {
    55             for (int i = 0; i <Ans.size()&&i<max_count; i++)
    56                 cout << Ans[i].Name << " " << Ans[i].Age << " " << Ans[i].Net_Worth << endl;
    57         }
    58     }
    59 }
    View Code

    改了之后还是没过。。。。这次是第二个点 看人家用的是数组 我用的Vector ......

     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include <climits>
     3 #include<iostream>
     4 #include<vector>
     5 #include<queue>
     6 #include<map>
     7 #include<set>
     8 #include<stack>
     9 #include<algorithm>
    10 #include<string>
    11 #include<cmath>
    12 using namespace std;
    13 struct Person{
    14     string Name;
    15     int Age;
    16     int Net_Worth;
    17 };
    18 bool compare(const Person& a, const Person& b)
    19 {
    20     return (a.Net_Worth != b.Net_Worth) ? a.Net_Worth > b.Net_Worth :
    21         (a.Age != b.Age) ? a.Age < b.Age : a.Name < b.Name;
    22 }
    23 int main()
    24 {
    25     ios::sync_with_stdio(false);
    26     int N, K;
    27     cin >> N >> K;
    28     vector<Person> V(N);
    29     for (int i = 0; i < N; i++)
    30         cin >> V[i].Name >> V[i].Age >> V[i].Net_Worth;
    31     sort(V.begin(), V.end(), compare);
    32     for (int i = 1; i <= K; i++)
    33     {
    34         int max_count, Amin, Amax;
    35         cin >> max_count >>Amin >> Amax;
    36         cout << "Case #" << i << ":" << endl;
    37         int count = 0;
    38         for (int i = 0; i < N; i++)
    39         {
    40             if (V[i].Age >= Amin && V[i].Age <= Amax)
    41             {
    42                 cout << V[i].Name << " " << V[i].Age << " " << V[i].Net_Worth << endl;
    43                 count++;
    44             }
    45             if (count == max_count)
    46                 break;
    47         }
    48         if (!count)
    49             cout << "None" << endl;
    50     }
    51 }
    View Code
  • 相关阅读:
    Django ORM 进行查询操作和性能优化
    Python PIL 长文本换行,二维码,图片合成
    python 常用的资料链接
    人生苦短,我用Python
    windows下搭建Python virtualenvvirtualenvwrapper虚拟环境。
    Python 文件上传base64图片
    python实现中文转换url编码的方法
    同时装了Python3和Python2,怎么用pip?
    统计当天下单量
    Django ORM 级联删除
  • 原文地址:https://www.cnblogs.com/57one/p/12051174.html
Copyright © 2011-2022 走看看