PAT(甲級)1055
阿新 • • 發佈:2019-02-15
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:<h1 style="margin: 0px 0px 0.5em; padding: 0px; border: 0px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; font-size: 3em; font-weight: normal; font-stretch: inherit; line-height: 1em; vertical-align: baseline; color: rgb(17, 17, 17); background-color: rgb(250, 250, 250);">1055. The World's Richest (25)</h1><div id="problemInfo" class="rfloat" style="margin: 0px; padding: 2px; border: 1px solid rgb(221, 221, 221); font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; font-stretch: inherit; line-height: 18px; vertical-align: baseline; float: right; text-align: center; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);"><div class="limit" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);">時間限制</div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">400 ms </div></div><div class="limit" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);">記憶體限制</div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">128000 kB </div></div><div class="limit" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);">程式碼長度限制</div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">16000 B </div></div><div class="checker" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;"><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);">判題程式</div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;"><span title="純文字對比" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">Standard</span></div></div><div class="key" style="margin: 0px; padding: 0px 20px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline; background-color: rgb(221, 221, 221);">作者</div><div class="value" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">CHEN, Yue </div></div><div id="problemContent" style="margin: 0px; padding: 0px; border: 0px; font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; font-stretch: inherit; line-height: 18px; vertical-align: baseline; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">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.</p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;"><strong>Input Specification:</strong></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=10<sup style="line-height: 0;">5</sup>) - the total number of people, and K (<=10<sup style="line-height: 0;">3</sup>) - 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<sup style="line-height: 0;">6</sup>, 10<sup style="line-height: 0;">6</sup>]) 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.</p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;"><strong>Output Specification:</strong></p><p style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: inherit;font-size:undefined; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; vertical-align: baseline;">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</p><pre style="margin-top: 1.5em; margin-bottom: 1.5em; padding: 0px; border: 0px; font-family: 'Droid Sans Mono', Consolas, 'Courier New', monospace; font-size: 1em; font-stretch: normal; line-height: 1.5; vertical-align: baseline; overflow: auto;">Name Age Net_Worth
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 50Sample 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define SIZE 100005
//////////////////////////////////////////////////////////////////
//1.choose proper model,just sort once, and judge every item queroed
// whether they are in the right age range
//2. use static variable to avoid repeat iterator the same iterm
//
////////////////////////////////////////////////////////////////////
using namespace std;
struct Person{
char name[10];
int age;
int wealth;
};
bool cmp(const Person &p1,const Person &p2){ //comparison must use const reference
if(p1.wealth < p2.wealth)
return false;
else if(p1.wealth == p2.wealth){
if(p1.age < p2.age){
return true;
}else if(p1.age == p2.age){
int key = strcmp(p1.name,p2.name);
if(key < 0)
return true;
else
return false;
}else
return false;
}else
return true;
}
Person records[SIZE];
int find(int l,int h,int n,bool flag){
static int i = 0; //can avid duplicated iterator
if(flag)
i =0;
for(;i<n;i++){
if(records[i].age <= h && records[i].age >=l){
i++;
return i-1;
}
}
if(i == n)
return -1;
}
int main()
{
int N,K;
int i,num;
scanf("%d%d",&N,&K);
for(i=0;i<N;i++){
scanf("%s%d%d",&records[i].name,&records[i].age,&records[i].wealth);
}
num = i;
sort(records,records+num,cmp);
int times,age_l,age_h;
for(i=1;i<=K;i++){
scanf("%d%d%d",×,&age_l,&age_h);
printf("Case #%d:\n",i);
int mark = find(age_l,age_h,num,true);
if(mark == -1){
printf("None\n");
continue;
}else{
printf("%s %d %d\n",records[mark].name,records[mark].age,records[mark].wealth);
for(int j = 1;j<times;j++){
mark = find(age_l,age_h,num,false);
if(mark == -1)
break;
else
printf("%s %d %d\n",records[mark].name,records[mark].age,records[mark].wealth);
}
}
}
}