1055 The World's Richest (25分)+测试点分析+思路分析

编程入门 行业动态 更新时间:2024-10-19 06:20:28

1055 The World's Richest (25分)+测试点分析+<a href=https://www.elefans.com/category/jswz/34/1769825.html style=思路分析"/>

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 (≤10​5​​) - the total number of people, and K (≤10​3​​) - 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 [−10​6​​,10​6​​]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤100) - 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 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

作者: CHEN, Yue
单位: 浙江大学
时间限制: 500 ms
内存限制: 128 MB
代码长度限制: 16 KB

解决方法

分析:排序
这个排序的思路是将所有人按题目所给要求排序。题目要求见cmp函数。然后的题目进行k次查询,输出在年龄区间内,前m个人的信息。

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
struct person
{char name[20];int age;int money;
};
bool cmp(person a, person b)
{if (a.money != b.money) return a.money > b.money;else if (a.age != b.age) return a.age < b.age;else return strcmp(a.name, b.name) < 0;
}
int main()
{int n, k;scanf("%d %d", &n, &k);int book[205] = { 0 };vector<struct person>vec(n),v;for (int i = 0; i < n; i++) scanf("%s %d %d", vec[i].name, &vec[i].age, &vec[i].money);sort(vec.begin(), vec.end(), cmp);for (int i = 0; i < n; i++){if (book[vec[i].age] < 100){book[vec[i].age]++;v.push_back(vec[i]);}}int num, ageleft, ageright;for (int i = 0; i < k; i++){int flag = 0;scanf("%d %d %d", &num, &ageleft, &ageright);printf("Case #%d:\n", i + 1);for (int j = 0; j < v.size(); j++){if (v[j].age <= ageright && v[j].age >= ageleft && flag < num){printf("%s %d %d\n", v[j].name, v[j].age, v[j].money);flag++;}}if (flag == 0) printf("None\n");}return 0;
}

测试点分析

这道题按照暴力解法双for循环一般除了测试点2和3因为超时问题,其余的基本上都能过。
超时问题是因为m的范围只是在100内,意思就是每个年龄中财富在前一百的人才能有机会输出。所以先把所有的人按照财富值排序,建立1个数组book标记每个年龄段拥有的人的数量,遍历数组并统计相应年龄的人数,当前年龄人数不超过100压入新的数组。即只取每个年龄前100名.

♦为什么book数组的大小是205呢?查询语句最多103次,而人的数据最多105次,我猜他肯定是直接取年龄范围0-200,这样就导致每次都是108数量级以上的次数,肯定会超时。

后记

对于超时问题,可以采用预处理的方法。这道题捣鼓了四五个小时,题目难度不大,主要是测试点的问题,是个好题。

更多推荐

1055 The World's Richest (25分)+测试点分析+思路分析

本文发布于:2024-02-07 02:46:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1752598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:思路   测试   World   Richest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!