• pku3418 Double Queue


    需要这样一个数据结构,支持如下操作

    1:插入优先级为p,编号为k的节点

    2:查询优先级最高的节点,输出编号并删除

    3:查询优先级最低的节点,输出编号并删除

    用一颗SBT即可完美解决问题,没什么好说的,多说无益~~~

    View Code
      1 program pku3481(input,output);
      2 var
      3     left,right,key,s,th:array[0..200000] of longint;
      4     tot,root:longint;
      5 procedure left_rotate(var t:longint);
      6 var
      7     k:longint;
      8 begin
      9     k:=right[t];
     10     right[t]:=left[k];
     11     left[k]:=t;
     12     s[k]:=s[t];
     13     s[t]:=s[left[t]]+s[right[t]]+1;
     14     t:=k;
     15 end;{ left_rotate }
     16 procedure right_rotate(var t:longint);
     17 var
     18     k:longint;
     19 begin
     20     k:=left[t];
     21     left[t]:=right[k];
     22     right[k]:=t;
     23     s[k]:=s[t];
     24     s[t]:=s[left[t]]+s[right[t]]+1;
     25     t:=k;
     26 end;{ right_rotate }
     27 procedure maintain(var t:longint;flag:boolean);
     28 begin
     29     if not flag then
     30     begin
     31         if s[left[left[t]]]>s[right[t]] then
     32             right_rotate(t)
     33         else
     34             if s[right[left[t]]]>s[right[t]] then
     35             begin
     36                 left_rotate(left[t]);
     37                 right_rotate(t);
     38             end
     39             else
     40                 exit;
     41     end
     42     else
     43     begin
     44         if s[right[right[t]]]>s[left[t]] then
     45             left_rotate(t)
     46         else
     47             if s[left[right[t]]]>s[left[t]] then
     48             begin
     49                 right_rotate(right[t]);
     50                 left_rotate(t);
     51             end
     52             else
     53                 exit;
     54     end;
     55     maintain(left[t],false);
     56     maintain(right[t],true);
     57     maintain(t,false);
     58     maintain(t,true);
     59 end;{ maintain }
     60 procedure insert(var now,k,u:longint);
     61 begin
     62     if now=0 then
     63     begin
     64         inc(tot);
     65         now:=tot;
     66         s[now]:=1;
     67         left[now]:=0;
     68         right[now]:=0;
     69         key[now]:=k;
     70         th[now]:=u;
     71     end
     72     else
     73     begin
     74         inc(s[now]);
     75         if k<key[now] then
     76             insert(left[now],k,u)
     77         else
     78             insert(right[now],k,u);
     79         maintain(now,k>=key[now]);
     80     end;
     81 end;{ insert }
     82 function delete(var t:longint;k:longint):longint;
     83 begin
     84     dec(s[t]);
     85     if (k=key[t])or((k<key[t])and(left[t]=0))or((k>key[t])and(right[t]=0)) then
     86     begin
     87         delete:=key[t];
     88         if left[t]*right[t]=0 then
     89             t:=left[t]+right[t]
     90         else
     91             key[t]:=delete(left[t],k+1);
     92     end
     93     else
     94         if k<key[t] then
     95             delete:=delete(left[t],k)
     96         else
     97             delete:=delete(right[t],k);
     98 end;{ delete }
     99 function get_max_number(t:longint):longint;
    100 begin
    101     if t=0 then
    102         exit(19950714);
    103     get_max_number:=get_max_number(right[t]);
    104     if get_max_number=19950714 then
    105         get_max_number:=t;
    106 end;{ get_max_number }
    107 function get_min_number(t:longint):longint;
    108 begin
    109     if t=0 then
    110         exit(19950714);
    111     get_min_number:=get_min_number(left[t]);
    112     if get_min_number=19950714 then
    113         get_min_number:=t;
    114 end;{ get_min_number }
    115 procedure main;
    116 var
    117     x,y,z:longint;
    118 begin
    119     root:=0;
    120     tot:=0;
    121     read(x);
    122     while x<>0 do
    123     begin
    124         if x=1 then
    125         begin
    126             readln(y,z);
    127             insert(root,z,y);
    128         end;
    129         if x=2 then
    130             if s[root]=0 then
    131                 writeln(0)
    132             else
    133             begin
    134                 y:=get_max_number(root);
    135                 writeln(th[y]);
    136                 delete(root,key[y]);
    137             end;
    138         if x=3 then
    139             if s[root]=0 then
    140                 writeln(0)
    141             else
    142             begin
    143                 y:=get_min_number(root);
    144                 writeln(th[y]);
    145                 delete(root,key[y]);
    146             end;
    147         read(x);
    148     end;
    149 end;{ main }
    150 begin
    151     main;
    152 end.                
  • 相关阅读:
    【数学建模】—优秀论文(一)
    【数学建模】—论文排版
    【Linux学习】—第8章linux编程
    【Linux学习】—文件权限和目录配置
    【ESP8266学习】(一)
    【OpenCV】——b站达尔闻
    【Linux学习】——Shell编程基础
    【数学建模】——模拟退火算法(SAA)
    react 开发中火狐,Safari浏览器嵌套iframe显示空白
    element ui dataPicker 日期范围限制
  • 原文地址:https://www.cnblogs.com/neverforget/p/2482948.html
Copyright © 2020-2023  润新知