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 1021. Sacrament of the Sum

Help, why my algoritm no corect!
Posted by MozG 5 Nov 2008 14:48
System send, "Wrong Answer" test # 3! Why? And if replace (N1 > N2)- (N1 >= N2) OK?

PROGRAM B;

{$APPTYPE CONSOLE}

uses
  SYSUTILS;

VAR
  Mas1, Mas2 : ARRAY[1..50000] OF INTEGER;
  j : INTEGER;
  N1, N2 : INTEGER;
  Flag2 : BOOLEAN;
  Net : BOOLEAN;

PROCEDURE Search(Flag : BOOLEAN; L, R : INTEGER);

VAR
  C : INTEGER;

BEGIN
  WHILE ((L <= R) AND (Flag2 = FALSE) AND (Net = FALSE)) DO
    BEGIN
      C := ((L + R) DIV 2);
      IF Flag THEN
        BEGIN
          IF ((Mas2[j] + Mas1[C]) = 10000) THEN
            Flag2 := TRUE
          ELSE
            BEGIN
              IF ((Mas2[j] + Mas1[C]) > 10000) THEN
                Search(TRUE, L, (C - 1))
              ELSE
                Search(TRUE, (C + 1), R);
            END;
          Net := TRUE;
        END
      ELSE
        BEGIN
          IF ((Mas1[j] + Mas2[C]) = 10000) THEN
            Flag2 := TRUE
          ELSE
            BEGIN
              IF ((Mas1[j] + Mas2[C]) > 10000) THEN
                Search(TRUE, L, (C - 1))
              ELSE
                Search(TRUE, (C + 1), R);
            END;
          Net := TRUE;
        END;
    END;
END;

BEGIN
  {$IFNDEF ONLINE_JUDGE}
  ASSIGNFILE(INPUT, 'Input.txt');
  ASSIGNFILE(OUTPUT, 'Output.txt');
  {$ENDIF}

  RESET(INPUT);
  REWRITE(OUTPUT);

  ReadLn(Input, N1);

  FOR j := 1 TO N1 DO
    ReadLn(Input, Mas1[j]);

  ReadLn(Input, N2);

  FOR j := 1 TO (N2 - 1) DO
    ReadLn(Input, Mas2[j]);
  Read(Input, Mas2[N2]);

  j := 1;
  Net := FALSE;

  IF (N1 > N2) THEN
    BEGIN
      REPEAT
        Flag2 := FALSE;
        Search(TRUE, 1, N1);
        j := j + 1;
        Net := FALSE;
      UNTIL (Flag2 OR (j > N2));
    END
  ELSE
     BEGIN
      REPEAT
        Flag2 := FALSE;
        Search(FALSE, 1, N2);
        j := j + 1;
        Net := FALSE;
      UNTIL (Flag2 OR (j > N1));
    END;

  IF (Flag2 = TRUE) THEN
    Write(Output, 'YES')
  ELSE
    Write(Output, 'NO');

  {$IFNDEF ONLINE_JUDGE}
  CLOSE(INPUT);
  CLOSE(OUTPUT);
  {$ENDIF}
END.

Edited by author 05.11.2008 15:02