PAT_1055 world richest 多键排序
2014-03-06 19:24
246 查看
卡时间卡得真的很紧,自己写了下,还是有两个地方不能通过,显示的是超时了。
时间限制
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
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;
}
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;
}
相关文章推荐
- PAT 1055. The World's Richest (25) 年龄排序问题(O(n)排序)
- 1055. The World's Richest (25)【排序】——PAT (Advanced Level) Practise
- PAT甲题题解-1055. The World's Richest (25)-终于遇见一个排序的不水题
- PAT 1055. The World's Richest (25)(净资产排序,然后按照年纪分类输出m个)
- PAT (Advanced Level) 1055. The World's Richest (25) 年龄区间内最有钱的人,结构体排序
- PAT1055___排序神题
- PAT乙级 1055. 集体照 (25) 结构体字符串字典排序,双端队列
- Pat - 甲级 - 1055. The World's Richest (25)(排序)
- PAT乙级 1060. 爱丁顿数(25)--数学问题,sort()排序要注意的问题
- 1028. List Sorting (25)【排序】——PAT (Advanced Level) Practise
- PAT1055:The World's Richest
- PAT 1055. The World's Richest (25)
- PAT 1075. PAT Judge (25) 排序题,细节处理题
- 1075. PAT Judge (25)【排序】——PAT (Advanced Level) Practise
- PAT 1055. The World's Richest
- PAT甲级 1055. The World's Richest (25)
- pat乙级 1051-1055
- PAT乙级 1055. 集体照 (25)
- PAT甲题题解-1028. List Sorting (25)-水排序
- PAT - 甲级 - 1062. Talent and Virtue (25)(排序)