Cod sursa(job #875647)

Utilizator atatomirTatomir Alex atatomir Data 10 februarie 2013 16:12:27
Problema Elementul majoritar Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.78 kb
var n,cont,curent,i,x:longint;
    a:array[1..1000001]of longint;
    bufin:array[1..65000]of byte;

begin
  assign(input,'elmaj.in'); reset(input);
  assign(output,'elmaj.out'); rewrite(output);
  settextbuf(input,bufin);

  readln(n);

  read(curent); cont := 1; a[1] := curent;

  for i := 2 to n do
  begin
    read(a[i]);

    if (cont = 0)and(curent<>a[i]) then
    begin
      curent := a[i];
      inc(cont);
    end
    else
    begin
     if a[i] = curent then
        inc(cont)
      else
        dec(cont);
     end;
  end;

  if cont <= 0 then
    write('-1')
  else
  begin
    write(curent,' ');
    cont := 0;
    for i := 1 to n do
      if a[i] = curent then inc(cont);
    write(cont);
  end;

  close(input);
  close(output);
end.