1. 程式人生 > >1055 The World's Richest(25 分)

1055 The World's Richest(25 分)

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
原本自己做的總是超時,看了大神的才知道,純用c會快很多,用c++的話要改演算法。
這是我原來超時的演算法

#include <bits/stdc++.h>
using namespace std;

struct Node
{
	string name;
	int age;
	int worth;
}node[100005];
bool cmp1(Node a,Node b)
{
	if(a.age!=b.age)
		return a.age<b.age;
	else 
		return a.name<b.name;
}
bool cmp2(Node a,Node b)
{
	return a.worth>b.worth;
}
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)
	{
		cin>>node[i].name;
		scanf("%d%d",&node[i].age,&node[i].worth);
	}
	sort(node,node+n,cmp1);
	std::vector<Node> v;
	for(int i=1;i<=k;i++)
	{
		v.clear();
		printf("Case #%d:\n",i);
		int m,minimum,maximum;
		scanf("%d%d%d",&m,&minimum,&maximum);
		int j=0;
		while(j<n&&node[j].age<minimum)j++;
		for(;j<n;j++)
		{
			if(node[j].age>maximum)
				break;
			v.push_back(node[j]);
		}
		sort(v.begin(),v.end(),cmp2);
		for(int l=0;l<v.size()&&l<m;l++)
		{
			cout<<v[l].name<<" "<<v[l].age<<" "<<v[l].worth<<endl;
		}
		if(v.size()==0)
			printf("None\n");
	}
	return 0;
}

解題思路:
1.用結構體陣列記錄各項資訊,設定cmp排序函式。
2.按要求讀入,並排序。
3.根據輸出對人數和年齡段的限制,遍歷排序後的陣列,符合要求就輸出,達到制定人數就退出。
4.每一個查詢,設定cnt記錄已經輸出的人數,如果為0,要額外輸出None

原文:https://blog.csdn.net/chenyutingdaima/article/details/82254217

#include <bits/stdc++.h>
using namespace std;

struct Node
{
	char name[10];
	int age;
	int worth;
}node[100005];
bool cmp(Node a,Node b)
{
	if(a.worth!=b.worth)
		return a.worth>b.worth;
	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);
	for(int i=0;i<n;i++)
	{
		scanf("%s%d%d",node[i].name,&node[i].age,&node[i].worth);
	}
	sort(node,node+n,cmp);
	for(int i=1;i<=k;i++)
	{
		printf("Case #%d:\n",i);
		int m,minimum,maximum;
		scanf("%d%d%d",&m,&minimum,&maximum);
		int flag=0,cnt=0;
		for(int l=0;l<n;l++)
		{
			if(node[l].age>=minimum&&node[l].age<=maximum)
			{
				cout<<node[l].name<<" "<<node[l].age<<" "<<node[l].worth<<endl;
				flag=1;
				cnt++;
			}
			if(cnt==m)
				break;
		}
		if(!flag)
			printf("None\n");
	}
	return 0;
}

原文:https://blog.csdn.net/liuchuo/article/details/52225204

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
struct node {
    char name[10];
    int age, money;
};
int cmp1(node a, node 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, num, amin, amax;
    scanf("%d %d", &n, &k);
    vector<node> vt(n), v;
    vector<int> book(205, 0);
    for(int i = 0; i < n; i++)
        scanf("%s %d %d", vt[i].name, &vt[i].age, &vt[i].money);
    sort(vt.begin(), vt.end(), cmp1);
    for(int i = 0; i < n; i++) {
        if(book[vt[i].age] < 100) {
            v.push_back(vt[i]);
            book[vt[i].age]++;
        }
    }
    for(int i = 0; i < k; i++) {
        scanf("%d %d %d", &num, &amin, &amax);
        vector<node> t;
        for(int j = 0; j < v.size(); j++) {
            if(v[j].age >= amin && v[j].age <= amax)
                t.push_back(v[j]);
        }
        if(i != 0) printf("\n");
        printf("Case #%d:", i + 1);
        int flag = 0;
        for(int j = 0; j < num && j < t.size(); j++) {
            printf("\n%s %d %d", t[j].name, t[j].age, t[j].money);
            flag = 1;
        }
        if(flag == 0) printf("\nNone");
    }
    return 0;
}