Cod sursa(job #504787)

Utilizator mraresMardare Rares mrares Data 28 noiembrie 2010 18:23:13
Problema Secventa Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <cstdio>
#define nmax 5000000
#define inf -100000

using namespace std;

FILE *fin=fopen("secventa.in", "r");
FILE *fout=fopen("secventa.out", "w");

int v[nmax], deque[nmax];
int front, back, n, k, maxim, pozi, pozj;

int main()
{
    int i;
    maxim = inf;
    fscanf(fin, "%d%d", &n, &k);
    for(i=1; i<=n; ++i)
        fscanf(fin, "%d", &v[i]);

    front = 1;
    for(i=1; i<=n; ++i)
    {
        while(front<=back && (i-deque[back]>=k || v[i]<=v[deque[back]])) --back;
        deque[++back] = i;


        while(front<=back && i-deque[front]>=k) ++front;
        if(i == k-1)
        {
            maxim=v[deque[front]];
            pozi = 0;
            pozj = k-1;
        }
        if(i>=k)
            if(maxim<v[deque[front]])
            {
                maxim=v[deque[front]];
                pozi = i-k+1;
                pozj = pozi+k-1;
            }
    }

    fprintf(fout, "%d %d %d", pozi,  pozj, maxim);
}