Cod sursa(job #35599)

Utilizator Bluedrop_demonPandia Gheorghe Bluedrop_demon Data 22 martie 2007 11:23:57
Problema Pachete Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 4.05 kb
{
    Problema Pachete
}

Program Pachete;

Type numere = array[1..50000,1..2] of  Longint;

Var c1, c2, c3, c4 : numere;
    n, i, j, n1, n2, n3, n4, y : Longint;
    Ox, Oy, Dr : Longint;

Function pozitie( st, dr, signum : integer; Var nr : numere ) : Longint;
Var i, j, piv, aux1, aux2 : Longint;
Begin
    piv := signum*nr[st,1];
    i := st-1;
    j := dr+1;
    Repeat
        Repeat i := i+1; Until signum*nr[i,1] >= piv;
        Repeat j := j-1; Until signum*nr[j,1] <= piv;
        If i < j then
            Begin
                aux1 := nr[i,1];
                aux2 := nr[i,2];
                nr[i,1] := nr[j,1];
                nr[i,2] := nr[j,2];
                nr[j,1] := aux1;
                nr[j,2] := aux2;
            End;
    Until i >= j;
    pozitie := j;
End;

Procedure qs( st, dr, signum : integer; Var nr : numere );
Var m : Longint;
Begin
    If st = dr then exit;
    m := pozitie( st, dr, signum, nr );
    qs( st, m, signum, nr );
    qs( m+1, dr, signum, nr );
End;

Begin
    Assign( input, 'pachete.in' );
    Reset( input );
        Readln( n );
        Readln( Ox, Oy );
        n1 := 0;
        n2 := 0;
        n3 := 0;
        n4 := 0;
        While n > 0 do
            Begin
                Readln( i, j );
                if ( i >= Ox ) and ( j > Oy ) then
                    Begin
                        n1 := n1+1;
                        c1[n1,1] := i;
                        c1[n1,2] := j;
                    End
                    else
                if ( i < Ox ) and ( j >= Oy ) then
                    Begin
                        n2 := n2+1;
                        c2[n2,1] := i;
                        c2[n2,2] := j;
                    End
                    else
                if ( i <= Ox ) and ( j < Oy ) then
                    Begin
                        n3 := n3+1;
                        c3[n3,1] := i;
                        c3[n3,2] := j;
                    End
                    else
                if ( i > Ox ) and ( j <= Oy ) then
                    Begin
                        n4 := n4+1;
                        c4[n4,1] := i;
                        c4[n4,2] := j;
                    End;
                n := n-1;
            End;
    Close( input );
    if n1 > 0 then qs( 1, n1, 1, c1 );
    if n2 > 0 then qs( 1, n2, -1, c2 );
    if n3 > 0 then qs( 1, n3, -1, c3 );
    if n4 > 0 then qs( 1, n4, 1, c4 );

    dr := 0;

    n := n1;
    While n > 0 do
    Begin
        dr := dr+1;
        n := n-1;
        y := Oy;
        For i := 1 to n1 do
            If c1[i,1] > -1 then
                If c1[i,2] > y then
                    Begin
                        n := n-1;
                        c1[i,1] := -1;
                        y := c1[i,2];
                    End;
    End;
    n := n2;
    While n > 0 do
    Begin
        dr := dr+1;
        n := n-1;
        y := Oy;
        For i := 1 to n2 do
            If c2[i,1] > -1 then
                If c2[i,2] > y then
                    Begin
                        n := n-1;
                        c2[i,1] := -1;
                        y := c2[i,2];
                    End;
    End;
    n := n3;
    While n > 0 do
    Begin
        dr := dr+1;
        n := n-1;
        y := Oy;
        For i := 1 to n3 do
            If c3[i,1] > -1 then
                If c3[i,2] < y then
                    Begin
                        n := n-1;
                        c3[i,1] := -1;
                        y := c3[i,2];
                    End;
    End;
    n := n4;
    While n > 0 do
    Begin
        dr := dr+1;
        n := n-1;
        y := Oy;
        For i := 1 to n4 do
            If c1[i,1] > -1 then
                If c4[i,2] < y then
                    Begin
                        n := n-1;
                        c4[i,1] := -1;
                        y := c4[i,2];
                    End;
    End;


    Assign( output, 'pachete.out' );
    Rewrite( output );
        Writeln( dr );
    Close( output );
End.