Cod sursa(job #689099)

Utilizator oancea_horatiuOancea Horatiu oancea_horatiu Data 24 februarie 2012 09:28:10
Problema Elementul majoritar Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 0.62 kb
var maj,i,j,n,k:longint;
    s:array [1..1000000] of longint;
    o,d:text;
begin
assign(o,'elmaj.out');rewrite(o);
assign(d,'elmaj.in');reset(d);
read(d,n);
for i:=1 to n do
  begin
    read(d,s[i]);
    if k=0 then
      begin
        k:=k+1;
        maj:=s[i];
      end else if s[i]=maj then
                 begin
                   k:=k+1;
                 end
                 else begin
                        k:=k-1;
                      end;
  end;
j:=0;
for i:=1 to n do if s[i]=maj then j:=j+1;
if j>(n div 2) then write(o,maj,' ',j)
               else writeln(o,-1);
close(o);close(d);
end.