|  | 
|  | 
| вернуться в форум | Is it possible to use radix sort? I was trying to use radix sort but I got MLE (2 array [1..150000] and  one [0..100], all of longint).I not very familiar with radix sort and I was wondering if there's a way to sort the data in the same array I'm storing them.
 
 Thanks!
Just counting sort :) (-)Re: Just counting sort :) (-) I'm still having troubles. My program can sort all the data by m, but for equal m the output doesn't necesarly goes in the same order as in the input. E.g., for the sample input I get this (lines 2 and 3 are swapped):3 5
 22 4
 26 4
 16 3
 20 3
 1 2
 11 2
 7 1
 
 I could have them order correctly if I could use one extra array but I can not find the way to do it in the same array. Is there a way to do that, or do I have to go through the array of data 101 times? Here's my code:
 
 Var
 i,m,n,t: Longint;
 s: array [0..100] of longint;
 d: array [1..150000] of longint;
 
 Begin
 readln(input,n);
 for i:= 1 to n do
 begin
 readln(input,t,m);
 Inc(s[m]);
 d[i]:= -(1000*t+m);
 end;
 for i:= 1 to 100 do
 Inc(s[i],s[i-1]);
 for i:= 1 to n do
 begin
 while (d[i]<0) do
 begin
 d[i]:= -d[i];
 m:=  d[i] mod 1000;
 t:= d[s[m]]; d[s[m]]:= d[i]; d[i]:= t;
 Dec(s[m]);
 end;
 end;
 for i:= n downto 1 do
 writeln(output,d[i] div  1000,' ', d[i] mod  1000);
 End.
Re: Just counting sort :) (-) Послано mkd  30 апр 2004 02:49You don't have to sort anything. Just read input and write output :Pcode like:
 
 read teams
 for i=100 to 0 do
 write teams where score==i
 
 ;P
 
 mkd
Re: Just counting sort :) (-) Thanks, I got AC using that. But I still think there must be a better way...another solution Послано mkd  30 апр 2004 05:08Well, it wouldn't work if score could be in range 0..500 or greater. But you are right - there is a way to solve it even with such ranges.You'd just have to store team's IDs with the same score in some linked list. In order to avoid MLE a list element should have around 500 team IDs. Then you just print these lists for each score.
 It works (I tried it) but it's not much faster for the 1100 test data (0.3 sec, while the previous solution 0.35 sec).
 
 mkd
 | 
 | 
|