【PAT】1055. The World's Richest
来源:互联网 发布:java socket 心跳机制 编辑:程序博客网 时间:2024/04/29 06:57
关键:预处理,注意到M的范围只有100,排序后重开一个数组记录在该年龄前一百的记录,从而数据量只有10000,而不是原来的100000,启示我们优化可以从数据量入手,比如这里年龄只有0到100,每个年龄的数据量最多100从而找到优化的地方
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Student{ char name[10]; int age,worth;}stu[100010],valid[100010];bool cmp(Student a,Student 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;}int Age[100010];int main(){ int n,m; cin>>n>>m; for(int i=0;i<n;i++){ scanf("%s %d%d",stu[i].name,&stu[i].age,&stu[i].worth); } sort(stu,stu+n,cmp); int validNum=0; for(int i=0;i<n;i++){ if(Age[stu[i].age]<100){ valid[validNum++]=stu[i]; Age[stu[i].age]++; } } for(int i=0;i<m;i++){ int num,low,high; cin>>num>>low>>high; printf("Case #%d:\n",i+1); int printNum=0; for(int j=0;j<validNum && printNum<num;j++){ if(valid[j].age<=high && valid[j].age>=low){ printf("%s %d %d\n",valid[j].name,valid[j].age,valid[j].worth); printNum++; } } if(printNum==0) cout<<"None"<<endl; } 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)
- <PAT>1055. The World's Richest
- PAT 1055. The World's Richest
- PAT: 1055. The World's Richest (25)
- PAT 1055. The World's Richest (25)
- pat 1055. The World's Richest (25)
- PAT 1055. The World's Richest (25)
- PAT 1055. The World's Richest
- 【PAT】1055. The World's Richest
- PAT--1055. The World's Richest
- PAT 1055. The World's Richest (25)
- 1055. The World's Richest (25)-PAT
- PAT (Advanced) 1055. The World's Richest (25)
- 【C++】PAT(advanced level)1055. The World's Richest (25)*
- PAT A 1055. The World's Richest (25)
- 1055. The World's Richest (25)-PAT甲级真题
- 【PyCharm 教程】(六)自定义文件模板
- HDU-1087 Super Jumping! Jumping! Jumping! (DP)
- Ubuntu命令打印本机ip
- Spark单机模式搭建碰到的port异常
- CentOS 7、阿里云CentOS 6.5安装Postgre 9.6
- 【PAT】1055. The World's Richest
- 《概率统计与随机过程》——笔记1
- QT显式调用VC开发的DLL
- Tango 开发指南 — 事件
- 继续聊聊HDFS BlockManager扩展性问题
- 【PyCharm 教程】(七)管理解释器及第三方模块、包
- 微信小程序-使用audio组件播放音乐
- 单链表的基本操作(python)
- NTC热敏电阻