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 [Amin
, Amax
] 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 [Amin
, Amax
]. 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 4Zoe_Bill 35 2333Bob_Volk 24 5888Anny_Cin 95 999999Williams 30 -22Cindy 76 76000Alice 18 88888Joe_Mike 32 3222Michael 5 300000Rosemary 40 5888Dobby 24 5888Billy 24 5888Nobody 5 04 15 454 30 354 5 951 45 50
Sample Output:
Case #1:Alice 18 88888Billy 24 5888Bob_Volk 24 5888Dobby 24 5888Case #2:Joe_Mike 32 3222Zoe_Bill 35 2333Williams 30 -22Case #3:Anny_Cin 95 999999Michael 5 300000Alice 18 88888Cindy 76 76000Case #4:None 第一个代码是用vector存储,但遍历超时,第二个时使用数组存储,测试通过. 搞不明白同样的算法复杂度,为什么vector遍历时间就比数组差那么多?
1 #include2 #include 3 #include 4 #include 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 else19 return a.val > b.val;20 }21 int main()22 {23 cin >> N >> K;24 vector v;25 vector >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 #include2 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 >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 0;++j)//遍历数组27 if(person[j].age>=Amin&&person[j].age<=Amax){ //找到符合要求的人28 printf("%s %d %d\n",person[j].name.c_str(),person[j].age,person[j].worth);//输出29 --M;//将M递减30 f=true;//表示已输出过31 }32 if(!f)//在该年龄段没有任何输出33 printf("None\n");//输出None34 }35 return 0;36 }