Page 44 - C:\Users\Admin\Desktop\Sach mem upweb\
P. 44
100 Problems & Solutions Trang 44
Pos[2, i] := i;
end;
end;
procedure QuickSort(t, l, r: Integer);
var
x, tg, i, j: Integer;
begin
x := Val[t, (l + r) div 2];
i := l; j := r;
repeat
while Val[t, i] < x do Inc(i);
while Val[t, j] > x do Dec(j);
if i <= j then
begin
Tg := Val[t, i]; Val[t, i] := Val[t, j]; Val[t, j] := Tg;
Tg := Pos[t, i]; Pos[t, i] := Pos[t, j]; Pos[t, j] := Tg;
Inc(i); Dec(j);
end;
until i > j;
if i < r then QuickSort(t, i, r);
if j > l then QuickSort(t, l, j);
end;
procedure WriteOutput;
var
i: Integer;
Sum: LongInt;
hf: Text;
begin
Sum := 0;
for i := 1 to n do Inc(Sum, Val[1, n - i + 1] * Val[2, i]);
for i := 1 to n do Val[1, Pos[1, n - i + 1]] := Pos[2, i];
Assign(hf, Out);
Rewrite(hf);
Writeln(hf, Sum);
for i := 1 to n do Writeln(hf, Val[1, i]);
Close(hf);
end;
begin
ReadInput;
QuickSort(1, 1, n);
QuickSort(2, 1, m);
WriteOutput;
end.
Bài 3/1999 - Mạng tế bào
(Dành cho học sinh THPT)
Program Bai3/1999;
uses crt;
const fi = 'P3.inp';
Tin học & Nhà trường 100 Đề Toán - Tin học