Cod sursa(job #1664605)

Utilizator medicinedoctoralexandru medicinedoctor Data 26 martie 2016 13:44:20
Problema Elementul majoritar Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 1 kb
var a,q:array [1..1000000] of longint;
n,p:longint;

procedure elmaj;
var i,j:longint; x:boolean;
begin
  for i:=1 to n do
  begin
    if p=0 then begin p:=1; q[p]:=a[i]; end else
    begin
      x:=true;
      for j:=p downto 1 do
        if a[i]<>q[j] then begin x:=false; q[j]:=0; p:=p-1; break; end;
      if x then begin p:=p+1; q[p]:=a[i]; end;
    end;
  end;
end;

procedure lire;
var i:longint;
begin
  assign(input,'elmaj.in');
  reset(input);
  read(n);
  for i:=1 to n do read(a[i]);
  close(input);
end;

function co(var y:longint):longint;
var i,x:longint; b:boolean;
begin
  x:=0; b:=false;
  for i:=1 to n do
  begin
    if not(b) and (q[i]<>0) then begin b:=true; y:=q[i]; end;
    if q[i]<>0 then x:=x+1;
  end;
  co:=x;
end;

procedure ecrire;
var q,x:longint;
begin
  assign(output,'elmaj.out');
  rewrite(output);
  x:=co(q);
  if q=0 then write(-1) else write(q,' ',x+(n div 2)-1+(n mod 2));
  close(output);
end;

begin
  lire;
  elmaj;
  ecrire;
end.