Cod sursa(job #1428840)

Utilizator casianos1996Marc Casian Nicolae casianos1996 Data 5 mai 2015 10:25:16
Problema Problema Damelor Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.16 kb
program siruri;
var     val,s,max,k,j,p,i,n:longint;
        nr:array[1..1000000] of longint;
        a,v:array[1..1000000] of char;
        bufin,bufout:array[1..1 shl 16] of byte;
        f,g:text;


function cauta(c:char; st,dr:longint):longint;
var      m:longint;
begin
  while st<=dr do
    begin
      m:=(st+dr) div 2;
      if c>=a[m] then dr:=m-1
        else  st:=m+1
    end;
  cauta:=st;
end;

begin
  assign(f,'1-siruri.in'); reset(f);
  assign(g,'siruri.out'); rewrite(g);
  settextbuf(f,bufin); settextbuf(g,bufout);
  readln(f,p);
  readln(f,n);
  for i:=1 to n do
    read(f,v[i]);
  if p=1 then
    begin
      //I'm still working on it!
      nr[n]:=1;
      s:=1;
      for i:=n-1 downto 1 do
        begin
          nr[i]:=1;
          for j:=i+1 to n do
            if v[i]<=v[j] then nr[i]:=(nr[i]+nr[j]) mod 41357;
          s:=(s+nr[i]) mod 41357;
        end;
      writeln(g,s);
    end
  else
    begin
      k:=1;
      a[1]:=v[1];
      for i:=2 to n do
        begin
          j:=cauta(v[i],1,k);
          a[j]:=v[i];
          if j>k then k:=j;
        end;
      writeln(g,k);
    end;
  close(g);
end.