Cod sursa(job #465325)

Utilizator SpiderManSimoiu Robert SpiderMan Data 23 iunie 2010 21:07:44
Problema Cautare binara Scor 60
Compilator fpc Status done
Runda Arhiva educationala Marime 3.14 kb
program cautarebinara ;

const FIN = 'cautbin.in';
      FOU = 'cautbin.out';

type vec = string [255] ;

var N, i, T, aux, cnt, a, val, k, l : longint ;
    V : array[1 .. 100005] of longint ;
    f, g : text ;
    x : vec ;

function parse ( var x : vec ; var a : longint ) : longint ;
    var i , y : longint;

    begin
            i := a; y := 0;

            while (i <= k) do
                begin
                    if ( x[i] >= '0' ) and ( x[i] <= '9' ) then
                        y := y * 10 + ord(x[i]) - 48
                    else
                        begin
                            if ( i = 255 ) then
                                begin
                                    a := 1 ; i := 0;
                                    read ( f, x ) ;
                                    k := length ( x ) ;
                                end ;
                            inc ( i ) ;
                            break;
                        end;

                    if ( i = 255 ) then
                         begin
                              a := 1 ; i := 0;
                              read ( f, x ) ;
                              k := length ( x ) ;
                         end ;

                    inc ( i ) ;

                end;

           a := i;
           parse := y;
  end;

    begin
        assign ( f, FIN ) ; reset ( f ) ;
        assign ( g, FOU ) ; rewrite ( g ) ;

        readln ( f, N ) ;

        read ( f, x ) ; k := length ( x ) ; l := 1;

        for i := 1 to N do
          begin  V[i] := parse ( x, l ) ;writeln(V[i]) end; //read ( f, V[i] ) ;//V[i] := parse ( x, l ) ;

        readln ( f ) ; readln ( f, T ) ;

        aux := 1 ;

        while ( aux <= N ) do
            aux := aux shl 1 ;


        while ( T <> 0 ) do
            begin
                readln ( f, x ) ; k := length ( x ) ; l := 1;
                a := parse ( x, l ) ; val := parse ( x, l ) ;//readln ( f, a, val ) ;

                if ( a <> 2 ) then
                    begin
                        cnt := aux; i := 0 ;

                        while ( cnt <> 0 ) do
                            begin
                                if ( i + cnt <= N ) and ( V[i + cnt] <= val ) then
                                    inc ( i, cnt ) ;
                                cnt := cnt shr 1 ;
                            end;

                        if ( a = 0 ) and ( V[i] = val ) or ( a = 1 ) then
                            writeln ( g, i )
                        else writeln ( g, '-1' ) ;
                    end
                else
                    begin
                        cnt := aux; i := 0 ;

                        while ( cnt <> 0 ) do
                            begin
                                if ( i + cnt <= N ) and ( V[i + cnt] < val ) then
                                    inc ( i, cnt ) ;
                                cnt := cnt shr 1 ;
                            end;

                       writeln ( g, i + 1 ) ;
                    end;
               dec ( T ) ;
            end;

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