Cod sursa(job #216943)

Utilizator chelaru_t_achelaru traian andrei chelaru_t_a Data 26 octombrie 2008 12:16:54
Problema Heavy metal Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.14 kb
type hm=record
        a,b:longint;
        end;
var v:array [1..100000] of hm;
    n:longint;
    f,g:text;

 procedure qsort(a,b:longint);
  var i,j:longint;
      x,aux:hm;
  begin
   i:=a;
   j:=b;
   x:=v[(i+j) div 2];
   repeat
   while (v[i].a<x.a) and (v[i].b<x.b) do i:=i+1;
   while (v[j].a>x.a) and (v[j].b>x.b) do j:=j-1;
   if i<=j then
     begin
     aux:=v[i];
     v[i]:=v[j];
     v[j]:=aux;
     i:=i+1;
     j:=j-1;
     end;
   until i>j;
   if a<j then qsort(a,j);
   if b>i then qsort(i,b);
  end;

 procedure citire;
  var i:longint;
  begin
   readln(f,n);
   for i:=1 to n do readln(f,v[i].a,v[i].b);
  end;

 procedure rez;
  var i,s:longint;
  begin
   for i:=2 to n do
     if v[i-1].b>v[i].a then
       if v[i-1].b>v[i].b then
         begin
         v[i]:=v[i-1];
         v[i-1].a:=0;
         v[i-1].b:=0;
         end
       else v[i].a:=v[i-1].b;
   s:=0;
   for i:=1 to n do s:=s+v[i].b-v[i].a;
   write(g,s);
  end;

begin
  assign(f,'heavymetal.in');
  assign(g,'heavymetal.out');
  reset(f);
  rewrite(g);
  citire;
  qsort(1,n);
  rez;
  close(f);
  close(g);
end.