ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1064. Binary Search

Help! Why I got WA? (my program inside)
Posted by XiaoTian 12 Dec 2002 17:12
I tried many cases and got the same answers as another ACed program.
But I still got WA. Why?


{No.1064 Binary Search}
const
  MAXN = 10000;
var
  A: array[0..MAXN-1] of Longint;
  N,i,L: longint;
  Flag:Array[1..MAXN] Of Boolean;
  Duan,X,Y,Int1:LongInt;
  Bln1,IsFound:Boolean;
{-----Begin of original code-----}
procedure BinarySearch(x: LongInt);
var
  p, q, i: Longint;
begin
  p := 0;   { Left border of the search  }
  q := N-1; { Right border of the search }
  L := 0;   { Comparison counter         }
  while p <= q do begin
    i := (p + q) div 2;
    inc(L);
    if A[i] = x then begin
{      writeln('Found item i = ', i,
        ' in L = ', L, ' comparisons');}
      exit
    end;
    if x < A[i] then
      q := i - 1
    else
      p := i + 1
  end;
  IsFound:=False;
end;
{-----End of original code-----}
Begin
     ReadLn(X,Y);

     For i:=1 To MAXN Do
          A[i-1]:=i-1;
     FillChar(Flag,SizeOf(Flag),False);
     For N:=X To MAXN Do
     Begin
          IsFound:=True;
          BinarySearch(X);
          If Not IsFound Then Continue;
          If L=Y Then Flag[N]:=True;
     End;

     Duan:=0;
     Bln1:=False;
     For i:=1 To MAXN Do
     Begin
          If Flag[i] And Not Bln1 Then
               Inc(Duan);
          Bln1:=Flag[i];
     End;

     WriteLn(Duan);
     Int1:=-1;
     Bln1:=False;
     For i:=1 To MAXN Do
     Begin
          If Flag[i] And Not Bln1 Then
               Int1:=i;
          If Bln1 And Not Flag[i] Then
          Begin
               WriteLn(Int1,' ',i-1);
               Int1:=-1;
          End;
          Bln1:=Flag[i];
     End;
     If Flag[MAXN] Then
          WriteLn(Int1,' ',MAXN);
End.
Don't waste your time to read my problem. I resubmited it and got AC! Maybe I typed a wrong problem number last time.
Posted by XiaoTian 16 Dec 2002 15:24