Cod sursa(job #254697)

Utilizator MihaiBunBunget Mihai MihaiBun Data 7 februarie 2009 13:46:43
Problema Cuburi2 Scor 0
Compilator fpc Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.86 kb
program bgbg;
var f,g:text;
    a:array[1..250000] of longint;
    n,m,i,j,mij,x,y,indice,x1,y1:longint;
    s1,s2,min:int64;

begin
  assign(f,'cuburi2.in');
  assign(g,'cuburi2.out');
  rewrite(g);
  reset(f);
  readln(f,n,m);
  for i:=1 to n do read(f,a[i]);

  readln(f);

  for j:=1 to m do
  begin
     readln(f,x,y);
       min:=9223372036854775807;
       x1:=x;
       y1:=y;
     repeat
       mij:=(x1+y1) div 2;
       s1:=0;
       for i:=x to mij-1 do s1:=s1+(mij-i)*a[i];
       s2:=0;
       for i:=mij+1 to y do s2:=s2+(i-mij)*a[i];
       if (s1+s2)<min then begin
                            min:=s1+s2;
                            indice:=mij
                           end;
       if s1<s2 then x1:=mij+1
                else y1:=mij-1;

     until x1>y1;
     writeln(g,indice,' ',min);
  end;
  close(f);
  close(g);
end.