Cod sursa(job #772727)

Utilizator hungntnktpHungntnktp hungntnktp Data 30 iulie 2012 16:36:13
Problema Deque Scor 60
Compilator fpc Status done
Runda Arhiva educationala Marime 1.12 kb
Const
        tfi     =       'deque.in';
        tfo     =       'deque.out';
        nmax    =       5000000;
Type
        arr1    =       array[1..nmax] of longint;
Var
        fi,fo   :       text;
        n,k,f,r :       longint;
        a,q     :       arr1;
        res     :       int64;
Procedure mo;
  Begin
        assign(fi,tfi);reset(fi);
        assign(fo,tfo);rewrite(fo);
  End;
Procedure dong;
  Begin
        close(fi);
        close(fo);
  End;
Procedure push(x:longint);
  Begin
        inc(r);
        q[r]:=x;
  End;
Procedure nhap;
  Var
        i       :       longint;
  Begin
        read(fi,n,k);
        For i:=1 to n do read(fi,a[i]);
        f:=1;
        r:=0;
  End;
Procedure xuly;
  Var
        i,j     :       longint;
  Begin
        For i:=1 to n do
          Begin
                 While (f<=r) and (a[i]<=a[q[r]]) do dec(r);
                 push(i);
                 if (q[f]=i-k) then inc(f);
                 If i>=k then res:=res+a[q[f]];
          End;
        writeln(fo,res);
  End;
BEGIN
        mo;
        nhap;
        xuly;
        dong;
END.