PDA

View Full Version : Dãy con cấp số cộng dài nhất



hoasentrang_cm
13-04-2014, 13:03
Bài toán tìm dãy con CSC dài nhất với công sai d
Cho dãy N số tự nhiên (2≤N≤1000). Hãy tìm trong dãy con dài nhất tạo thành CSC có công sai d.

hoasentrang_cm
13-04-2014, 13:04
procedure xuli;
begin
QuickSort(1,n);
for i:= 1 to n do write(a[i], ' ');
a[0]:=a[1]-d;
a[n+1]:=a[n]+d; {khoi tao 2 bien rao 2 dau}
L[n+1]:=1;
for i:= n downto 0 do
begin
jmax:=n+1;
for j:=i+1 to n+1 do
if (a[j]=a[i]+d) and (L[j]>L[jmax])then
jmax:=j;
L[i]:=L[jmax]+1;
T[i]:=jmax;
end;
end;

procedure ghi;
var f:text;
begin
assign(F,fo);
rewrite(f);
writeln(f,L[0]-2);
i:=T[0];
while i<>n+1 do
begin
write(f,a[i], ' ');
i:=T[i];
end;
close(f);
end;
Mình đã thử nhiều input nhưng với bộ Input sau thì cho kq sai
16 3
1 9 7 4 1 6 2 4 9 7 11 8 15 11 10 11