1 type 2 node=record 3 data,time:longint; 4 end; 5 var 6 a:array[1..100]of longint; 7 b,c:array[1..100]of node; 8 i,j,t,n,h,k:longint; 9 procedure push_max(m:longint); 10 var 11 i,k:longint; 12 begin 13 k:=t+1; 14 for i:=t downto h do 15 if a[m]>c[i].data then 16 begin 17 c[i+1].data:=0; 18 c[i+1].time:=0; 19 c[i].data:=a[m]; 20 c[i].time:=m; 21 dec(k); 22 end 23 else break; 24 if k=t+1 then 25 begin 26 inc(t); 27 c[t].data:=a[m]; 28 c[t].time:=m; 29 end 30 else t:=k; 31 end; 32 procedure push_min(m:longint); 33 var 34 i,k:longint; 35 begin 36 k:=t+1; 37 for i:=t downto h do 38 if a[m]<b[i].data then 39 begin 40 b[i+1].data:=0; 41 b[i+1].time:=0; 42 b[i].data:=a[m]; 43 b[i].time:=m; 44 dec(k); 45 end 46 else break; 47 if k=t+1 then 48 begin 49 inc(t); 50 b[t].data:=a[m]; 51 b[t].time:=m; 52 end 53 else t:=k; 54 end; 55 procedure pop_min(m:longint); 56 var 57 i,k:longint; 58 begin 59 if b[h].time<m then 60 begin 61 b[h].data:=0; 62 b[h].time:=0; 63 inc(h); 64 end; 65 end; 66 procedure pop_max(m:longint); 67 var 68 i,k:longint; 69 begin 70 if c[h].time<m then 71 begin 72 c[h].data:=0; 73 c[h].time:=0; 74 inc(h); 75 end; 76 end; 77 begin 78 readln(n,k); 79 for i:=1 to n do 80 read(a[i]); 81 h:=1; 82 t:=1; 83 b[1].data:=a[1]; 84 b[1].time:=1; 85 for i:=2 to k do 86 push_min(i); 87 write(b[h].data,‘ ‘); 88 for i:=k+1 to n do 89 begin 90 push_min(i); 91 pop_min(i-k+1); 92 write(b[h].data,‘ ‘); 93 end; 94 writeln; 95 h:=1; 96 t:=1; 97 c[1].data:=a[1]; 98 c[1].time:=1; 99 for i:=2 to k do 100 push_max(i); 101 write(c[h].data,‘ ‘); 102 for i:=k+1 to n do 103 begin 104 push_max(i); 105 pop_max(i-k+1); 106 write(c[h].data,‘ ‘); 107 end; 108 writeln; 109 end.
↑这是垃圾方法,苛刻数据会死一个点
下面是二分的方法:{还没编完,敬请期待}
和合并果子差不多,关键是要退队(^(* ̄(oo) ̄)^当然不是那个少先队。。)
时间: 2024-10-12 06:12:20