Cod sursa(job #820277)

Utilizator chimistuFMI Stirb Andrei chimistu Data 20 noiembrie 2012 17:34:44
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <cstdio>
#include <cstdlib>

FILE*f;
FILE*g;
#define maxn 5000001
int A[ maxn ],deq[ maxn ], poz[ maxn ];
long long s;
int n,k,i,st,dr,ind1,ind2,max;
int main()
{


    f=fopen("secventa.in","r");
    g=fopen("secventa.out","w");
    fscanf(f,"%d%d",&n,&k);s=0;
    for (i=1;i<=n;i++)
        fscanf(f,"%d",&A[i]);
    st=1;dr=0;max=A[1];ind1=1;ind2=1;
    for (i=1;i<=n;i++)
    {   while ((A[i]<=deq[dr]) && (st<=dr))
              dr--;
         deq[++dr]=A[i];
		 poz[dr] = i;
         if (poz[st]==i-k) st++;
         if ((i>=k) && (deq[st]>max)) {ind1=poz[st];ind2=poz[dr];max=deq[st];}
         }
    fprintf(g,"%d %d %d",ind1,ind2,max);
    return 0;
}