Cod sursa(job #964379)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 20 iunie 2013 19:26:36
Problema Elementul majoritar Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.68 kb
program elmaj;
  var bufin:array[1..100000]of char;
      a:array [1..1000000] of longint;
      n,i,ans,k:longint;

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

  readln(n);
  for i:=1 to n do
    begin
      read(a[i]);
      if ans=0 then
        begin
          ans:=a[i];
          k:=1;
        end else
      if ans<>a[i] then
        begin
          dec(k);
          if k=0 then ans:=0;
        end
        else inc(k);
    end;

  k:=0;
  for i:=1 to n do
    if a[i]=ans then inc(k);

  if k>=n div 2 +1  then writeln(ans,' ',k) else writeln(-1);
  close(output);
end.