1055. The World's Richest (25)
来源:互联网 发布:v29万能主板 数据 编辑:程序博客网 时间:2024/06/02 13:14
点击打开链接
又点会卡时间,因此必须进行优化
1.对所有人排序就要全面排序
2.排序后的total是符合答案排序准则的
3.因此对于ans的push过程就要加上对num的检验
#include <algorithm>#include <cstdio>#include <vector>#include <string.h>#include <string>using namespace std;int n,m;struct s {char name[10];int age;int worth;};vector <s> total;bool cmp(s a,s b){if(a.worth!=b.worth){return a.worth>b.worth;}else{if(a.age!=b.age){return a.age<b.age;}else{return strcmp(a.name,b.name)<0;}}}vector <s> ans;void push(int a,int b,int num){ans.clear();for(int i=0;i<n;i++){if(total[i].age>=a && total[i].age<=b){if(ans.size()<=num){ans.push_back(total[i]); //对于已经达到要求的,不用进行遍历,这样省时间 if(ans.size()==num){break;}}}}}int main(){freopen("in.txt","r",stdin);scanf("%d %d",&n,&m);for(int i=0;i<n;i++){s tmp;scanf("%s %d %d",tmp.name,&tmp.age,&tmp.worth);total.push_back(tmp);}sort(total.begin(),total.end(),cmp);for(int i=1;i<=m;i++){int ma,mb,num;scanf("%d %d %d",&num,&ma,&mb);push(ma,mb,num);//sort(ans.begin(),ans.end(),cmp);printf("Case #%d:\n",i);if(ans.size()==0){printf("None\n");}else{for(int j=0;j<ans.size();j++){printf("%s %d %d\n",ans[j].name,ans[j].age,ans[j].worth);}}}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)
- 图像处理--图像增强
- 【自考】操作系统之宏观认识
- 脚本获取 app 的 cpu、memory 信息,使用 pychartdir 生成图表
- 为什么Android的图片质量会比iPhone的差?
- Servlet 单例多线程
- 1055. The World's Richest (25)
- CentOS 下JDK安装
- 日经社説 20150206 混迷の中東にどう関与を深めるか
- 根据Android架构分层推荐开发书籍
- 欢迎使用CSDN-markdown编辑器
- cdh5.1.0 yum安装说明
- AKima 插值实现
- 程序员面试不应有的8个错误
- linux本机流量控制tc(traffic control)