您的位置:首页 > 编程语言 > C语言/C++

PAT_1055 world richest 多键排序

2014-03-06 19:24 246 查看
卡时间卡得真的很紧,自己写了下,还是有两个地方不能通过,显示的是超时了。


1055. The World's Richest (25)

时间限制

200 ms

内存限制

128000 kB

代码长度限制

16000 B

判题程序

Standard

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 (<=105) - the total number of people, and K (<=103) - 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 [-106, 106]) 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"
#include<iostream>

#include<vector>

#include<string>

#include<algorithm>

#include<fstream>

#include<functional>

using namespace std;

/**

N>=0 && N<=10e5; totalNumber;

K>=0 && K<=10e3; number of queries;

  M richest people with the range of [Amin,Amax],

  
no-increasing order of the net worths;

 if the worth equals, be in non-decreasing order of the ages;
 
if both same, in non-decreasing alpha of name.

 none one find, output "None";

*/

struct People{
string name;
int age;
int worth;

bool operator > (const People &p)const{
if( worth != p.worth){
return worth > p.worth;
}else{
if( age != p.age){
return age < p.age;
}else{
return name < p.name;
}
}
}

};

/*

inline bool compare(People person1, People person2){

if( person1.worth != person2.worth){

return person1.worth > person2.worth;

}else if( person1.age != person2.age ){

return person1.age < person2.age;

}else{

return person1.name < person2.name;

}

};

*/

int main(){

int N = 12; 
scanf("%d",&N);
//cin>>N;

int K = 4; 
scanf("%d",&K);
//cin>>K;

vector<People> people(N);

// ifstream fin("d:\\pat_data.txt");

int fi
ad4c
lter_num = 0;
int age_count[201] = {0};
bool age_over[201] = {false};

int okay_input[ (int)(10e6) ];

int okay_index = 0;

for(int nI = 0; nI < N; nI++){
// fin>>people[nI].name>>people[nI].age>>people[nI].worth;

char id[8];
scanf("%s",id);

people[nI].name.assign(id);

scanf("%d %d",&people[nI].age, &people[nI].worth);

if(++age_count[ people[nI].age ] <= 100){
okay_input[ okay_index++] = nI;
}

//cin>>people[nI].name>>people[nI].age>>people[nI].worth;
}

for(int kI = 0; kI < K; kI++){
int M,Amin,Amax;
//cin>>M>>Amin>>Amax;
scanf("%d %d %d",&M,&Amin,&Amax);

vector<People> ageOkay;

int minWorth = 10e6;
int minIndex = 0;

for(int vecI = 0; vecI < okay_index; vecI++){

int index = okay_input[ vecI ];
int age = people[index].age;

if(age_over[age]){
continue;
}

if( age <= Amax && age >= Amin){
if( ageOkay.size() >= M && people[index].worth < minWorth){
continue;
}else{
ageOkay.push_back(people[index]);
if(people[index].worth < minWorth){
minWorth = people[ index ].worth;
}
}
}
}

sort(ageOkay.begin(), ageOkay.end(), greater<People>());

M = M<ageOkay.size()? M: ageOkay.size();

printf("Case #%d:\n",kI+1);
if(ageOkay.size() == 0){
//cout<<"None"<<endl;
printf("None\n");
}else{
for(int i = 0; i<M; i++){
printf("%s %d %d\n",ageOkay[i].name.c_str(),ageOkay[i].age,ageOkay[i].worth);
//cout<<ans[i].name<<" "<<ans[i].age<<" "<<ans[i].worth<<endl;
}
}
}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  OJ PAT ZJU C++