这题90分,为什么TLE
  • 板块灌水区
  • 楼主S_Z_Xcoco
  • 当前回复9
  • 已保存回复9
  • 发布时间2023/9/8 20:57
  • 上次更新2023/11/2 22:16:58
查看原帖
这题90分,为什么TLE
918508
S_Z_Xcoco楼主2023/9/8 20:57

【题目描述】

有个朋友在医院工作,想请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≤100000 ,每个人的优先级都不一样,0≤ 优先级≤2000000000 。

姓名都是小写字母组成的,长度小于20 。

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans,heap_size;
inline ll read(){
    ll k=0;char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))k=(k<<1)+(k<<3)+(ch^48),ch=getchar();
    return k;
}
struct node{
    char name[20];
    ll ga;
}a,heap[300001],res;
void put(node d){
    ll now,next;
    heap[++heap_size]=d;
    now=heap_size;
    while(now>1){
        next=now/2;
        if(heap[now].ga<heap[next].ga){
            return;
        }
        swap(heap[now],heap[next]);
        now=next;
    }
}
node get(){
    ll now,next;
    res=heap[1];
    heap[1]=heap[heap_size--];
    now=1;
    while(now*2<=heap_size){
        next=now*2;
        if(next<heap_size&&heap[next+1].ga>heap[next].ga){
            next++;
        }
        if(heap[now].ga>heap[next].ga){
            return res;
        }
        swap(heap[now],heap[next]);
        now=next;
    }
    return res;
}
int main(){
    ll n;
    n=read();
    for(ll i=1;i<=n;i++){
        char orr[10];
        cin>>orr;
        if(strcmp(orr,"push")==0){
            cin>>a.name>>a.ga;
            put(a);
        }
        if(strcmp(orr,"pop")==0){
            if(heap_size==0){
                cout<<"none"<<endl;
            }
            else{
                cout<<heap[1].name<<" "<<heap[1].ga<<endl;
                get();
            }
        }
    }
    return 0;
}
2023/9/8 20:57
加载中...