Cod sursa(job #254594)

Utilizator razyelxrazyelx razyelx Data 7 februarie 2009 13:08:27
Problema Cuburi2 Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.9 kb
#include <stdio.h>
FILE*in = fopen("cuburi2.in","r");
FILE*out= fopen("cuburi2.out","w");

long long n,m,max1,max2;
long long t[250001];
long long cauta_poz(long long x,long long y){
    long i,poz1=1,poz2=1;
    max1=-1;
    max2=-1;

    for(i=x;i<=y;++i)
       if(t[i]>max1){max2=max1;max1=t[i];poz2=poz1;poz1=i;}
       else if(t[i]>max2){max2=t[i];poz2=i;}
    if(poz2>poz1+2) return poz1+1;
    else if(poz2<poz1-2) return poz1-1;
	 else return poz1;
}
long long abs(long long x){
     if(x<0) return -x;
     return x;
}
int main(){
    long long i,j,x,y,poz,s;

    fscanf(in,"%lld%lld",&n,&m);

    for(i=1;i<=n;++i)
       fscanf(in,"%lld",&t[i]);

    for(i=1;i<=m;++i){
       s=0;
       fscanf(in,"%lld%lld",&x,&y);
       poz = cauta_poz(x,y);

       for(j=x;j<=y;++j)
	  if(j!=poz)s+=abs(poz-j)*t[j];

       fprintf(out,"%lld %lld\n",poz,s);
    }
    return 0;
}