- 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
- 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
题目大意
给你N个人的信息 : name age rich
查询K次
每次输出年龄段[left,right]内,rich排前X名的人,rich相同比较age,age相同比较name
思路
见代码
-
- #include
- using namespace std;
- struct People{
- string name;
- int age,rich;
- }p;
- vector
age[201],result; - bool cmp(const People& x,const People& y){
- return x.rich != y.rich ? x.rich > y.rich : x.age!=y.age ? x.age
- }
- int main()
- {
-
-
- int N,M,left,right;
- cin >> N >> M;
- while (N--){
- cin >> p.name >> p.age >> p.rich;
- age[p.age].push_back(p);
- }
-
- for(int z=1;z<=200;z++) {
- sort(age[z].begin(),age[z].end(),cmp);
- while (age[z].size()>100) age[z].pop_back();
- }
-
- for(int z=1;z<=M;z++){
- printf("Case #%d:\n",z);
- cin >> N >> left >> right;
-
- while (left<=right){
- for(const People& x:age[left]) result.push_back(x);
- left++;
- if(result.size()>=N) break;
- }
-
- sort(result.begin(),result.end(),cmp);
- while (result.size()>N) result.pop_back();
-
- while (left<=right){
- for(const People& x: age[left]){
- if(x.rich<=result.back().rich) break;
- int flag = N;
- while (flag>0 && x.rich > result[flag-1].rich) flag--;
- result.insert(result.begin()+flag,x);
- result.pop_back();
- }
- left++;
- }
-
- for(const People& x: result) cout << x.name << " " << x.age << " " << x.rich << endl;
- if(result.empty()) puts("None");
- else result.clear();
- }
- return 0;
- }