小根/大根堆裸题模板

2162: 看病

时间限制: 1 Sec  内存限制: 128 MB
提交: 227  解决: 86
[提交][状态][讨论版]

题目描述

BSNY有个朋友在医院工作,想请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


 

 

来源

var
   n,x,i,t:longint;
   ch:char;
   st,s:string;
   f:boolean;
   a:array[0..500000] of longint;
   str:array[0..200000] of string[60];
procedure push(x:longint);
var
   tx,i:longint;
   z:string;
begin
 inc(t);
 a[t]:=x;
 str[t]:=st;
 i:=t;
 while (i>1)and(a[i>>1]<a[i]) do
  begin
   tx:=a[i>>1];
   a[i>>1]:=a[i];
   a[i]:=tx;
   z:=str[i>>1];
   str[i>>1]:=str[i];
   str[i]:=z;
   i:=i>>1;
  end;
end;
procedure pop(x:longint);
var
   tx,i,so:longint;
   z:string;
begin
 a[1]:=a[t];
 str[1]:=str[t];
 dec(t);
 i:=1;
 while (i*2<=t)or(i*2+1<=t) do
  begin
   if (i*2+1>t)or(a[i*2]>a[i*2+1]) then so:=i*2 else so:=i*2+1;
   if a[so]>a[i] then
    begin
     tx:=a[so];
     a[so]:=a[i];
     a[i]:=tx;
     z:=str[so];
     str[so]:=str[i];
     str[i]:=z;
     i:=so;
    end
   else break;
  end;
end;
begin
 readln(n);
 t:=0;
 for i:=1 to n do
  begin
   s:='';
   read(ch);
   f:=true;
   while ch<>' ' do
    begin
     s:=s+ch;
     if s='pop' then
      begin
       readln;
       if t=0 then writeln('none')
       else begin writeln(str[1],' ',a[1]);pop(1);end;
       f:=false;break;
      end;
     read(ch);
    end;
   if f=false then continue;
   read(ch);
   st:='';
   while ch<>' ' do
    begin
     st:=st+ch;
     read(ch);
    end;
   readln(x);
   push(x);
  end;
end.
/**************************************************************
    Problem: 2162
    User: 2014Summer8
    Language: Pascal
    Result: 正确
    Time:276 ms
    Memory:14088 kb
****************************************************************/
posted @ 2016-07-16 14:23  2014summer8  阅读(467)  评论(0)    收藏  举报