1055. The World's Richest (25)
来源:互联网 发布:qq for mac远程协助 编辑:程序博客网 时间:2024/06/07 01:10
题目链接:https://www.patest.cn/contests/pat-a-practise/1055
先进行排序,然后在排好序的vector中寻找符合条件的对象,否则会有case超时。
如果采用先寻找符合条件的对象,然后排序,会超时。。。。
#include <cstdio>#include <vector>#include <string>#include <algorithm>#include <iostream>#include <map>#include <numeric>using namespace std;struct Person{ string name; int age; int wealth; Person(){}};bool cmp(const Person &a, const Person &b){ if(a.wealth!=b.wealth)return a.wealth>b.wealth; else if(a.age!=b.age)return a.age<b.age; else if(a.name!=b.name)return a.name<b.name; return true;}vector<Person> persons;vector<int> ns,mins,maxs;int main(){ int num,queries; cin>>num>>queries; persons.resize(num); for(int i=0;i<num;i++)cin>>persons[i].name>>persons[i].age>>persons[i].wealth; sort(persons.begin() , persons.end(), cmp); int n,amin,amax; for(int i=0;i<queries;i++){ scanf("%d %d %d",&n,&amin,&amax); ns.push_back(n); mins.push_back(amin); maxs.push_back(amax); } sort(persons.begin() , persons.end() , cmp); for(int i=0;i<queries;i++){ printf("Case #%d:\n",i+1); int _size=persons.size(); int cnt=0; for(int j=0;j<_size;j++){ if(persons[j].age>=mins[i] && persons[j].age<=maxs[i]){ printf("%s %d %d\n", persons[j].name.c_str(),persons[j].age, persons[j].wealth); cnt++; if(cnt==ns[i])break; } } if(cnt==0)printf("None\n"); } /* for(int i=0;i<queries;i++){ vector<Person> output; for(vector<Person>::iterator it=persons.begin() ; it!=persons.end() ;++it){ if(it->age>=mins[i] && it->age<=maxs[i])output.push_back(*it); } int t=min(ns[i], (int)output.size()); printf("Case #%d:\n",i+1); if(t==0){printf("None\n");continue;} sort(output.begin(), output.end() ,cmp); for(int j=0;j<t;j++){ printf("%s %d %d\n",output[j].name.c_str(),output[j].age, output[j].wealth); } } */ return 0;}
阅读全文
0 0
- 【PAT】1055. The World's Richest (25)
- pat 1055. The World's Richest (25)
- pat 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- PAT: 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- PAT 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 团体程序设计天梯赛-练习集 L1-032. Left-pad
- VoLTE学习笔记(1)
- leetcode 树总结
- 数据结构-图-最短路径(2)弗洛伊德算法构造
- 表单脚本
- 1055. The World's Richest (25)
- git
- POJ2196 HDU1197 ZOJ2405 Specialized Four-Digit Numbers【进制+水题】
- Centos7.x 更改网卡名称eth0
- tornado异步
- 编译链接
- 基于高德SDK实现跑步时轨迹渐变功能
- 满足你使用bootstrap-table的基本需求 列表渲染 a标签的bug 添加按钮
- JAVA开发短信验证码系统