Cod sursa(job #1108445)

Utilizator DjokValeriu Motroi Djok Data 15 februarie 2014 18:08:45
Problema Elementul majoritar Scor 100
Compilator fpc Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.83 kb
var i,n,candidat,k:longint;
    a:array[1..1000001] of longint;
    buf1,buf2:array[1..1 shl 16] of char;
begin
assign(input,'elmaj.in');
assign(output,'elmaj.out');
reset(input);
rewrite(output);
settextbuf(input,buf1);
settextbuf(output,buf2);
   readln(n);
    for i:=1 to n do
     read(a[i]);

     candidat:=-1; k:=0;
      for i:=1 to n do
       if k=0 then begin
                    candidat:=a[i];
                    k:=1;
                   end
              else if candidat=a[i] then inc(k)
                                    else dec(k);
           k:=0;
            for i:=1 to n do
             if candidat=a[i] then inc(k);

              if k>=n div 2 + 1 then write(candidat,' ',k)
                                else write('-1');


close(input);
close(output);
{Totusi este trist in lume}
end.