Cod sursa(job #687186)

Utilizator promix2012petruta andrei promix2012 Data 22 februarie 2012 10:24:41
Problema Elementul majoritar Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.81 kb
program elmaj;
const fi='elmaj.in';
      fo='elmaj.out';
var f,g:text;
    bufin,bufout:array[1..65000] of char;
    v:array[1..1000000] of longint;
  n,i,cand,k,nr:longint;
begin
assign(f,fi);
reset(f);
assign(g,fo);
rewrite(g);
settextbuf(f,bufin);
settextbuf(f,bufout);
read(f,n);
for i:=1 to n do
   begin
   read(f,v[i]);
   end;
cand:=-1;
k:=0;
for i:=1 to n do
   if k=0 then
      begin
      cand:=v[i];
      k:=1;
      end
      else
        if v[i]=cand then
           inc(k)
           else
           dec(k);
if cand<>0 then
   begin
   nr:=0;
      for i:=1 to n do
         if v[i]=cand then
            inc(nr);
      if (nr>n/2) then
          writeln(g,cand,' ',nr)
          else
          writeln(g,-1);
   write(-1);
   end;
   close(f);
   close(g);
   end.