LIGHTS - Lights

Tác giả: ll931110

Ngôn ngữ: Pascal

program LIGHTS;
const
  input  = '';
  output = '';
  maxn = 10000;
var
  a,b: array[1..maxn] of boolean;
  n,step: integer;

procedure init;
var
  f: text;
  i: integer;
  ch: char;
begin
  assign(f, input);
    reset(f);

  readln(f, n);
  for i := 1 to n do
    begin
      read(f, ch);
      if ch = '0' then a[i] := false else a[i] := true;
    end;

  readln(f);
  for i := 1 to n do
    begin
      read(f, ch);
      if ch = '0' then b[i] := false else b[i] := true;
    end;

  close(f);
end;

procedure solve;
var
  i: integer;
begin
  step := 0;
  if n = 1 then
    begin
      if a[1] or b[1] then step := 1;
      exit;
    end;

  if (a[1] <> a[2]) and (b[1] <> b[2]) and a[1] and b[1] then
    begin
      inc(step);
      a[1] := not a[1];
      b[1] := not b[1];
    end;

  for i := 2 to n - 1 do
    if (a[i] <> a[i - 1]) and (b[i] <> b[i - 1])
      and (a[i] <> a[i + 1]) and (b[i] <> b[i + 1]) then
      begin
        inc(step);
        a[i] := not a[i];
        b[i] := not b[i];
      end;

  if (a[n] <> a[n - 1]) and (b[n] <> b[n - 1]) and a[n] and b[n] then
    begin
      inc(step);
      a[n] := not a[n];
      b[n] := not b[n];
    end;

  i := 1;
  while i <= n do
    begin
      while (i <= n) and not a[i] do inc(i);
      if i > n then break;

      inc(step);
      inc(i);
      while (i <= n) and a[i] do inc(i);
    end;

  i := 1;
  while i <= n do
    begin
      while (i <= n) and not b[i] do inc(i);
      if i > n then break;

      inc(step);
      inc(i);
      while (i <= n) and b[i] do inc(i);
    end;
end;

procedure printresult;
var
  f: text;
begin
  assign(f, output);
    rewrite(f);
    writeln(f, step);
  close(f);
end;

begin
  init;
  solve;
  printresult;
end.

Download