Cod sursa(job #1060422)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 17 decembrie 2013 23:20:59
Problema Elementul majoritar Scor 100
Compilator fpc Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 2.02 kb
Program elmaj;
 var a:array [1..1000001] of longint;
     b1:array [1..1 shl 17] of char;
     i,j,k,nr,n,aux,l,p:longint;
     s:string;
     fi,fo:text;
function citire:longint;
 var aux:longint;
begin
aux:=0;
 while (s[p]>='0') and (s[p]<='9') and (p<=l) do begin
                                            aux:=aux*10+ord(s[p])-48;
                                             if p=l then begin
                                                        read(fi, s);
                                                         p:=0;
                                                          l:=length(s);
                                                         end;
                                                      inc(p);
                                                  end;
 while ((s[p]<'0') or (s[p]>'9')) and (s<>'') do begin
                                                inc(p);
                                                if p>l then begin
                                                            read(fi, s);
                                                              p:=1;
                                                               l:=length(s);
                                                            end;
                                                  end;
citire:=aux;
end;
begin
 assign(fi,'elmaj.in');
  assign(fo,'elmaj.out');
 settextbuf(fi,b1);
 reset(fi); rewrite(fo);
 readln(fi,n); nr:=-1;
  read(fi,s); l:=length(s); p:=1;
for i:=1 to n do begin
                  a[i]:=citire;
                   if nr>=0 then begin
                                 if a[i]=k then inc(nr)
                                            else dec(nr);
                                   end
                            else begin
                                  k:=a[i]; nr:=1;
                                   end;
                  end; nr:=0;
 for i:=1 to n do
  if a[i]=k then inc(nr);
 if nr>=(n div 2)+1 then write(fo,k,' ',nr)
                  else write(fo,'-1');
close(fo);
end.