1055 The World’s Richest (25分)
吐槽
又是试了两种办法。然后才知道为啥有两个超时测点。我这两个方法擅长的测点正好不同。
思路
试了两种办法,一个测点1概率过测点2必过,另一个测点1必过测点2一直不过。
前者是一次排序,然后每次遍历所有人找出相应数量的符合年龄大小的人。
后者是先将所有人按年龄排序,然后每次找出那个年龄区间内的人(它们是相邻的),按worth排序/放进新vector后按worth排序,最后再重新按年龄排序该区间/舍弃那个新vector(“/”前后的两种用时差距不大,且都是1必过2不过)
当然还有传统艺能cin/cout改scanf/printf。
代码(1概率过2必过)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
typedef struct {
string name;
int age;
int worth;
}people;
bool cmp(people p1, people p2)
{
if (p1.worth == p2.worth)
if (p1.age == p2.age)
return p1.name < p2.name;
else
return p1.age < p2.age;
else
return p1.worth > p2.worth;
}
int main()
{
int n, k, i, ii, num, temp, temp2, count;
people tempp;
vector<people> v;
cin >> n >> k;
for (i = 0; i < n; i++)
{
cin >> tempp.name;
scanf("%d", &tempp.age);
scanf("%d", &tempp.worth);
v.push_back(tempp);
}
sort(v.begin(), v.end(), cmp);
for (i = 1; i <= k; i++)
{
printf("Case #%d:\n", i);
scanf("%d", &num);
scanf("%d", &temp);
scanf("%d", &temp2);
for (ii = 0, count = 0; ii < v.size() && count < num; ii++)
{
if (v[ii].age >= temp && v[ii].age <= temp2)
{
printf("%s %d %d\n", v[ii].name.c_str(), v[ii].age, v[ii].worth);
count++;
}
}
if (count == 0)
printf("None\n");
}
return 0;
}
代码(1必过2未过)(重sort版)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
typedef struct {
string name;
int age;
int worth;
}people;
bool cmp1(people p1, people p2)
{
return p1.age < p2.age;
}
bool cmp2(people p1, people p2)
{
if (p1.worth == p2.worth)
if (p1.age == p2.age)
return p1.name < p2.name;
else
return p1.age < p2.age;
else
return p1.worth > p2.worth;
}
int main()
{
int n, k, i, ii, iii, num, temp, temp2;
people tempp;
vector<people> v;
cin >> n >> k;
for (i = 0; i < n; i++)
{
cin >> tempp.name;
scanf("%d", &tempp.age);
scanf("%d", &tempp.worth);
v.push_back(tempp);
}
sort(v.begin(), v.end(), cmp1);
for (i = 1; i <= k; i++)
{
printf("Case #%d:\n", i);
cin >> num >> temp;
for (ii = 0; ii < v.size(); ii++)
if (v[ii].age >= temp)
break;
cin >> temp;
for (iii = ii; iii < v.size(); iii++)
if (v[iii].age > temp)
break;
if (ii == iii)
{
printf("None");
continue;
}
temp2 = iii;
sort(v.begin() + ii, v.begin() + iii, cmp2);
for (iii = 0; iii < num; iii++)
{
if (v[ii + iii].age > temp)
break;
printf("%s %d %d\n", v[ii + iii].name.c_str(), v[ii + iii].age, v[ii + iii].worth);
}
sort(v.begin() + ii, v.begin() + temp2, cmp1);
}
cin >> i;
return 0;
}
题目
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