Cod sursa(job #254515)

Utilizator razyelxrazyelx razyelx Data 7 februarie 2009 12:38:19
Problema Cuburi2 Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.75 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;
}

int main(){
    long long i,j,x,y,poz;

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

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

    for(i=1;i<=m;++i){
       fscanf(in,"%lld%lld",&x,&y);
       poz = cauta_poz(x,y);
       fprintf(out,"%lld\n",poz);
    }
    return 0;
}