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".
Sample Input:
12 4Zoe_Bill 35 2333Bob_Volk 24 5888Anny_Cin 95 999999Williams 30 -22Cindy 76 76000Alice 18 88888Joe_Mike 32 3222Michael 5 300000Rosemary 40 5888Dobby 24 5888Billy 24 5888Nobody 5 04 15 454 30 354 5 951 45 50
Sample Output:
Case #1:Alice 18 88888Billy 24 5888Bob_Volk 24 5888Dobby 24 5888Case #2:Joe_Mike 32 3222Zoe_Bill 35 2333Williams 30 -22Case #3:Anny_Cin 95 999999Michael 5 300000Alice 18 88888Cindy 76 76000Case #4:None
#include<iostream>#include<string>#include<vector>#include<algorithm>#include<string.h>using namespace std;struct person{ char name[10]; int age; int worth;};bool compare1(const person &a,const person &b){ return a.age<b.age;}bool compare2(const person &a,const person &b){ if(a.worth!=b.worth) return a.worth>b.worth; else if(a.age!=b.age) return a.age<b.age; if(strcmp(b.name,a.name)>0) return true; return false;}vector<person> v;vector<person> a;void qury(int b,int min,int max,int N){ int i; v.clear();/* int start=0,end=a.size()-1; while(start<end-1){ if(a[(start+end)/2].age>min){ end=(start+end)/2; } else if(a[(start+end)/2].age<min){ start=(start+end)/2; } else{ start=(start+end)/2; end=start; } } if(start-3<0) start=0; else start=start-3;*/ for(i=0;i<N;i++){ /*if(a[i].age>max) break;*/ if(a[i].age>=min&&a[i].age<=max) v.push_back(a[i]); if(v.size()>=b) break; } if(v.size()==0){ printf("None/n"); return ; } //sort(v.begin(),v.end(),compare2); if(b>=v.size()) for(i=0;i<v.size();i++){ printf("%s %d %d/n",v[i].name,v[i].age,v[i].worth); } else for(i=0;i<b;i++){ printf("%s %d %d/n",v[i].name,v[i].age,v[i].worth); }}int main(){ int N,M,i,temp1,temp2,temp3; person tt; cin>>N>>M; for(i=0;i<N;i++){ scanf("%s %d %d",&tt.name,&tt.age,&tt.worth); a.push_back(tt); } sort(a.begin(),a.end(),compare2); for(i=0;i<M;i++){ scanf("%d %d %d",&temp1,&temp2,&temp3); printf("Case #%d:/n",i+1); qury(temp1,temp2,temp3,N); }} 感想:这道题关键点是运行时间,测试点2和3表示数据量最大和查询数据量最大,具体要求:1. 先按输出要求用sort函数排序后,再取数据,这里当数据量到达了要求的量的时候要停止遍历,这样可以降低时间2. 不要用string类,不要用cout,用char* 和printf3. 使用vector也可以加快速度4. 注掉的部分是我之前的想法(先按时间排序,再用二分查找法找到要查找的子字符串的起始位置,然后再对字符串排序,结果会略超时)
新闻热点
疑难解答