Cod sursa(job #251082)

Utilizator MihaiBunBunget Mihai MihaiBun Data 1 februarie 2009 19:56:18
Problema Pavare2 Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.69 kb
program pava2;
var f:text;
    st,c:array[1..1000] of -1..1;
    n,a,b,i,j,k,nsol:integer;
    as,ev:boolean;
begin
  assign(f,'pavare2.in');
  reset(f);
  readln(f,n,a,b);
  readln(f,k);
  close(f);
  assign(f,'pavare2.out');
  rewrite(f);
  j:=1;
  st[j]:=-1;
  nsol:=0;
  while j>0 do
  begin
    repeat
      if st[j]<1 then begin
                       st[j]:=st[j]+1;
                       as:=true
                      end
                 else as:=false;
      if as then begin
                   if st[j]=0 then begin
                                     i:=j;
                                     while (st[i]=st[j]) and(i>0) do i:=i-1;
                                     if (j-i)<=a then ev:=true
                                              else ev:=false
                                   end
                               else begin
                                     i:=j;
                                     while (st[i]=st[j])and(i>0) do i:=i-1;
                                     if (j-i)<=b then ev:=true
                                              else ev:=false
                                   end
                 end
    until(not as) or (as and ev);
    if as then if j=n then begin
                              nsol:=nsol+1;
                              if nsol=k then
                                 for i:=1 to n do
                                    c[i]:=st[i]
                           end
                      else begin
                             j:=j+1;
                             st[j]:=-1
                           end
          else j:=j-1
  end;
writeln(f,nsol);
for i:=1 to n do write(f,c[i],' ');
close(f);
end.