Cod sursa(job #197857)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 6 iulie 2008 19:14:35
Problema Reconst Scor 35
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.83 kb
var a,b,s : array[1..100000] of longint;
    use : array[1..2000] of longint;
    i,j,n,m,ii,jj,nr,caz : longint;
    f,g : text;
procedure work1(i : longint);
var j,mm : longint;
Begin
  j:=i; mm:=m;
  repeat
      while a[j]=a[j+1] do inc(j);
      ii:=i;
          for jj:=ii+1 to j do
            if b[ii]<>b[jj] then
          begin
            a[jj]:=b[ii]+1;
            s[jj]:=s[jj]-s[ii];
              if a[jj]=b[jj] then
                if use[b[jj]]=0 then
                begin
                  use[b[jj]]:=s[jj]; inc(nr);
                end;
          end;
    inc(j); i:=j;
  until j>=mm;
End;

procedure work2(i : longint);
var j,mm : longint;
Begin
  j:=i; mm:=m;
  repeat
      while b[j]=b[j+1] do inc(j);
      ii:=i;
         for jj:=ii+1 to j do
            if a[ii]<>a[jj] then
          begin
            b[jj]:=a[jj]-1;
            a[jj]:=a[ii];
            s[jj]:=s[ii]-s[jj];
              if a[jj]=b[jj] then
                if use[b[jj]]=0 then
                begin
                  use[b[jj]]:=s[jj]; inc(nr);
                end;
          end;
    inc(j); i:=j;
  until j>=mm;
End;

procedure sort1(l,r : longint);
var i,j,x,y,aux : longint;
begin
     i:=l; j:=r; x:=a[(l+r)div 2];
     y:=b[(l+r)div 2];
     repeat
           while (a[i]<x)or((a[i]=x)and(b[i]<y))
            do inc(i);
           while (x<a[j])or((x=a[j])and(y<b[j]))
            do dec(j);
           if i<=j then
           begin
                aux:=a[i];a[i]:=a[j];a[j]:=aux;
                aux:=b[i];b[i]:=b[j];b[j]:=aux;
                aux:=s[i];s[i]:=s[j];s[j]:=aux;
                inc(i); dec(j);
           end;
     until i>j;
     if l<j then sort1(l,j);
     if i<r then sort1(i,r);
end;

procedure sort2(l,r : longint);
var i,j,x,y,aux : longint;
begin
     i:=l; j:=r; x:=a[(l+r)div 2];
     y:=b[(l+r)div 2];
     repeat
           while (b[i]<y)or((b[i]=y)and(a[i]<x))
            do inc(i);
           while (y<b[j])or((y=b[j])and(x<a[j]))
            do dec(j);
           if i<=j then
           begin
                aux:=a[i];a[i]:=a[j];a[j]:=aux;
                aux:=b[i];b[i]:=b[j];b[j]:=aux;
                aux:=s[i];s[i]:=s[j];s[j]:=aux;
                inc(i); dec(j);
           end;
     until i>j;
     if l<j then sort2(l,j);
     if i<r then sort2(i,r);
end;


begin
  assign(f,'reconst.in');reset(f);
  assign(g,'reconst.out');rewrite(g);
  read(f,n,m);
    for i:=1 to m do
      read(f,a[i],b[i],s[i]);
  i:=1; caz:=1;
    repeat
      if caz=1 then
      begin
        sort1(1,m);
        work1(i);
      end
      else
      begin
        sort2(1,m);
        work2(i);
      end;
      if caz=1 then caz:=2 else caz:=1;
    until nr=n;

  write(g,use[1]);
    for i:=2 to n do write(g,' ',use[i]);
  close(g);
end.