【CodeVS1076】排序

Description

给出n和n个整数,希望你从小到大给他们排序

Input

第一行一个正整数n

第二行n个用空格隔开的整数

Output

输出仅一行,从小到大输出n个用空格隔开的整数

Sample Input

3

3 1 2

Sample Output

1 2 3

Hint

1<=n<=100000

Solution

大材小用,Heap模板

 1 var
 2   f:array [1..1000000] of longint;  3  a,i,n,len:longint;  4  5 procedure swap(var a,b:longint);  6 var x:longint;  7 begin  8 x:=a;a:=b;b:=x;  9 end; 10 11 procedure insert(a:longint); 12 var 13  x:longint; 14 begin 15 inc(len); f[len]:=a; x:=len; 16 while (x>>1>0) and (f[x>>1]>f[x]) do 17 begin 18 swap(f[x],f[x>>1]); x:=x>>1; 19 end; 20 end; 21 22 function get:longint; 23 var 24  x:longint; 25 begin 26 get:=f[1]; f[1]:=f[len]; dec(len); x:=1; 27 while (x<<1<=len) and ((f[x<<1]or (f[x]>f[x<<1+1])) do 28 begin 29 if (f[x<<1]1+1]) or (x<<1=len) 30 then begin swap(f[x],f[x<<1]);x:=x<<1; end 31 else begin swap(f[x],f[x<<1+1]);x:=x<<1+1; end; 32 end; 33 end; 34 35 begin 36  readln(n); 37 len:=0; 38 for i:=1 to n do 39 begin 40  read(a); 41  insert(a); 42 end; 43 44 for i:=1 to n do 45 write(get,' '); 46  writeln; 47 end.


学长写的模板

 1 var
 2   a,h:array[0..100001]of longint;
 3   i,n,x,t,tmp:longint;
 4 
 5 procedure put(x:longint);
 6 var
 7   now:longint;
 8 begin
 9   inc(t); h[t]:=x; now:=t;
10   while (now<>1)and(h[now]> 1]) do
11     begin
12       tmp:=h[now]; h[now]:=h[now >> 1];
13       h[now >> 1]:=tmp; now:=now >> 1;
14     end;
15 end;
16 
17 function get:longint;
18 var
19   fa,son,now:longint;
20 begin
21   get:=h[1]; h[1]:=h[t]; dec(t);
22   fa:=1;
23   while fa << 1<=t do
24     begin
25       if (fa << 1=t)or(h[fa << 1]1+1])
26         then son:=fa << 1 else son:=fa << 1+1;
27       if h[son]>h[fa] then break;
28       tmp:=h[fa]; h[fa]:=h[son];
29       h[son]:=tmp; fa:=son;
30     end;
31 end;
32 
33 begin
34   readln(n);
35   for i:=1 to n do
36     begin read(x); put(x); end;
37   for i:=1 to n do write(get,' ');
38   writeln;
39 end.

 

转载于:https://www.cnblogs.com/raymondchen/p/6067668.html

你可能感兴趣的:(【CodeVS1076】排序)