Cod sursa(job #1409978)

Utilizator casianos1996Marc Casian Nicolae casianos1996 Data 30 martie 2015 19:55:54
Problema Elementul majoritar Scor 100
Compilator fpc Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.77 kb
program elmaj;
var     nr,x,n,m,c,i,j:longint;
        bufin,bufout:array[1..1 shl 16] of byte;
        v:array[1..1000001] of longint;
begin
  assign(input,'elmaj.in'); reset(input);
  assign(output,'elmaj.out'); rewrite(output);
  settextbuf(input,bufin);
  settextbuf(output,bufout);
  readln(n);
  read(v[1]);
  m:=1; c:=1;
  for i:=2 to n do
    begin
      read(v[i]);
      if v[i]=m then inc(c)
      else
        begin
          dec(c);
          if c=0 then
            begin
              m:=v[i];
              c:=1;
            end;
        end;
    end;
  close(input);
  reset(input);
  readln(n);
  nr:=0;
  for i:=1 to n do
    if v[i]=m then inc(nr);
  if nr>=(n div 2) +1 then writeln(m,' ',nr)
  else writeln('-1');
  close(output);
end.