Cod sursa(job #140109)

Utilizator vunvixvunvulea mariana vunvix Data 21 februarie 2008 12:23:53
Problema Nivele Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.93 kb
var v:array[1..50000]of longint;
t,n,i,j,m,k:longint;
ok,ok1,ch:boolean;
f,g:text;
begin
assign(f,'nivele.in');reset(f);
assign(g,'nivele.out');rewrite(g);
readln (f,t);
for i:=1 to t-1 do begin
 read(f,n);
 for j:=1 to n do read(f,v[j]);
 ok1:=false;
 ok:=true;
 m:=n;
 for j:=1 to n-1 do if (v[j]<>0) then begin
  k:=j;
  repeat
  k:=k+1;
  until (v[k]<>0)or (k>=n);
  if v[j]=v[k] then begin
  ok1:=true;
  m:=m-1;
  v[j]:=v[j]-1;
  if v[j]<1 then ok:=false;
  v[k]:=0;
  end;
  end;
 if not(ok1) then ok:=false;
 while ok and (m>1) do begin
  ch:=false;
  for j:=1 to n-1 do if  (v[j]<>0) then

  begin
  k:=j;
  repeat
  k:=k+1;
  until (v[k]<>0)or(k>n);
  if v[k]=v[j]then begin
  ch:=true;
  ok1:=true;
  m:=m-1;
  v[j]:=v[j]-1;
  if v[j]<1 then ok:=false;
  v[k]:=0;
  if not(ok1)and (m>1) then ok:=false;
  end;
  end;
 if not (ch)and (m>1) then ok:=false;
 end;
 m:=0;
 if ok then for i:=1 to n do if v[i]>0 then inc(m);
 if ok and (m>1) then ok:=false;
 if ok then writeln(g,'DA')else writeln(g,'NU');
 readln(f);
 end;

  read(f,n);
 for j:=1 to n do read(f,v[j]);
 ok1:=false;
 ok:=true;
 m:=n;
 for j:=1 to n-1 do if (v[j]<>0) then begin
  k:=j;
  repeat
  k:=k+1;
  until (v[k]<>0)or (k>=n);
  if v[j]=v[k] then begin
  ok1:=true;
  m:=m-1;
  v[j]:=v[j]-1;
  if v[j]<1 then ok:=false;
  v[k]:=0;
  end;
  end;
 if not(ok1) then ok:=false;
 while ok and (m>1) do begin
  ch:=false;
  for j:=1 to n-1 do if  (v[j]<>0) then

  begin
  k:=j;
  repeat
  k:=k+1;
  until (v[k]<>0)or(k>n);
  if v[k]=v[j]then begin
  ch:=true;
  ok1:=true;
  m:=m-1;
  v[j]:=v[j]-1;
  if v[j]<1 then ok:=false;
  v[k]:=0;
  if not(ok1)and (m>1) then ok:=false;
  end;
  end;
 if not (ch)and (m>1) then ok:=false;
 end;
 m:=0;
 if ok then for i:=1 to n do if v[i]>0 then inc(m);
 if ok and (m>1) then ok:=false;
 if ok then write(g,'DA')else write(g,'NU');

close(f);close(g);
end.