1371:看病
【题目描述】
有个朋友在医院工作,想请BSNY帮忙做个登记系统。具体是这样的,最近来医院看病的人越来越多了,因此很多人要排队,只有当空闲时放一批病人看病。但医院的排队不同其他排队,因为多数情况下,需要病情严重的人优先看病,所以希望BSNY设计系统时,以病情的严重情况作为优先级,判断接下来谁可以去看病。
【输入】
第一行输入n,表示有n个操作。
对于每个操作,首先输入push
或pop
。
push
的情况,之后会输入ai 和 bi,分别表示患者姓名和患者病情优先级。
pop
后面没有输入,但需要你输出。
【输出】
对于pop
的操作,输出此时还在排队人中,优先级最大的患者姓名和优先级。
表示他可以进去看病了。
如果此时没人在排队,那么输出”none
”,具体可见样例。
【输入样例】
7
pop
push bob 3
push tom 5
push ella 1
pop
push zkw 4
pop
【输出样例】
none
tom 5
zkw 4
【提示】
【数据规模和约定】
1≤n≤1000001,每个人的优先级都不一样,0≤优先级≤2000000000。
姓名都是小写字母组成的,长度小于20。
【思路】
优先队列
【代码】
#include<bits/stdc++.h>
using namespace std;
struct node{
string name;
int data;
friend bool operator<(node a,node b) {
return a.data<b.data;
}
};
priority_queue<node> h;
int main(){
int n;
cin>>n;
while(n--){
string op;
cin>>op;
if(op=="pop") {
if(!h.empty()){
node t=h.top();
h.pop();
cout<<t.name<<" "<<t.data<<"\n";
}
else
cout<<"none\n";
}
else{
node t;
cin>>t.name>>t.data;
h.push(t);
}
}
return 0;
}
千万别拿这个代码提交!!!
因为《试试就会逝世》
嘶——————
据说,优先队列的速度很快的呀
根据我的经验,这种超时1ms只需要把cin改成scanf就行了
于是。。。
#include<bits/stdc++.h>
using namespace std;
struct node{
string name;
int data;
friend bool operator<(node a,node b) {
return a.data<b.data;
}
};
priority_queue<node> h;
int main(){
int n;
scanf("%d",&n);
while(n--){
string op;
cin>>op;
if(op=="pop") {
if(!h.empty()){
node t=h.top();
h.pop();
cout<<t.name<<" ";
printf("%d\n",t.data);
}
else
puts("none");
}
else{
node t;
cin>>t.name>>t.data;
h.push(t);
}
}
return 0;
}
但是。。。
又“逝”了……
呜呜呜
好吧,我在这种情况下已经使用了玄学优化了——
friend bool operator<(const node &a,const node &b) {
中的
const node &a
和
const node &b
我真的谢谢……
在这种情况下,我只能去问老师了……
在问了老师之后,老师说:“你只需要把string换成char型数组,在用scanf读入字符串就可以了。”
emm……
好像确实是的。
然后,这份代码诞生了——
#include<bits/stdc++.h>
using namespace std;
struct node{
char name[25];
int data;
friend bool operator<(const node &a,const node &b) {
return a.data<b.data;
}
};
priority_queue<node> h;
int main(){
int n;
scanf("%d",&n);
while(n--){
char op[25];
scanf("%s",op);
if(op=="pop") {
if(!h.empty()){
node t=h.top();
h.pop();
printf("%s %d\n",t.name,t.data);
}
else
puts("none");
}
else{
node t;
scanf("%s%d",t.name,&t.data);
h.push(t);
}
}
return 0;
}
然鹅~
木有输出了……
嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷!!!!
经过我漫长的撕烤以后发现:
char op[25];
scanf("%s",op);
if(op=="pop")
这个位置写错了。啊啊啊!!!char数组怎么可以直接判等呢???
于是我找到了这个函数——strcmp
不会的可以参考一下这篇博文:https://blog.csdn.net/weixin_53564801/article/details/123730463
好吧,最后我们贴上正确代码
#include<bits/stdc++.h>
using namespace std;
struct node{
char name[25];
int data;
friend bool operator<(const node &a,const node &b) {
return a.data<b.data;
}
};
priority_queue<node> h;
int main(){
int n;
scanf("%d",&n);
while(n--){
char op[25];
scanf("%s",op);
if(strcmp(op,"pop")==0) {
if(!h.empty()){
node t=h.top();
h.pop();
printf("%s %d\n",t.name,t.data);
}
else
puts("none");
}
else{
node t;
scanf("%s%d",t.name,&t.data);
h.push(t);
}
}
return 0;
}
终于AC了!!!
谢谢大家的观看!(●'◡'●)(●'◡'●)(*^▽^*)(*^▽^*)