# Cod sursa(job #18428)

Utilizator Data 18 februarie 2007 12:09:35 Amlei 0 fpc done preONI 2007, Runda 2, Clasa a 10-a 1.21 kb
``````        var f,g:text;
n,t,u,i,k,j,aux,rr:longint;
a,b,c,d:array[-1..1000] of longint;
okp,ok,okt:boolean;
begin
assign(f,'amlei.in'); reset(f);
assign(g,'amlei.out'); rewrite(g);
while not eof(f) do begin
k:=1;
for i:=1 to t*n do begin
if a[i]<a[i-1] then begin
j:=i;
while (a[j]<a[j-1]) and (j>=k+1) do begin
aux:=a[j];
a[j]:=a[j-1];
a[j-1]:=aux;
dec(j);
end;
end;
if i mod n =0 then k:=i+1;
end;

k:=1;
for i:=1 to u*n do begin
if b[i]<b[i-1] then begin
j:=i;
while (b[j]<b[j-1]) and (j>=k+1) do begin
aux:=b[j];
b[j]:=b[j-1];
b[j-1]:=aux;
dec(j);
end;
end;
if i mod n = 0 then k:=i+1;
end;

if t>u then begin c:=b; b:=a; a:=c; aux:=t; t:=u; u:=aux; end;

okt:=true;
i:=1;
k:=n;
repeat
for j:=i to k do
c[j-i+1]:=a[j];
rr:=1;
ok:=false;
for j:=1 to n*u do begin
d[rr]:=b[j];
inc(rr);
if j mod n=0 then begin
okp:=true;
for rr:=1 to n do
if d[rr]<>c[rr] then okp:=false;
if okp=true then ok:=true;
rr:=1;
end;
end;
if ok=false then okt:=false;
i:=i+n;
k:=k+n;
until (k=t*u) or (okt=false);
if okt=true then writeln(g,'DA')
else writeln(g,'NU');
end;
close(f);
close(g);
end.
``````