Cod sursa(job #1462799)

Utilizator om6gaLungu Adrian om6ga Data 19 iulie 2015 00:24:37
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <stdio.h>

#define nmax 500005
#define SIZE 3500005

int n, k, baza, index;
int a[nmax], deque[nmax], i, j;
int first = 1, last = 0;
char string[SIZE];
char nr[20];

int main()
{
    FILE *in, *out;
    in  = fopen("secventa.in", "r");
    out = fopen("secventa.out", "w");
    
    fscanf(in, "%d %d\n", &n, &k);
    
    
    /*for (i = 1; i <= n; i++)
    {
        fgets(nr, 20, in);
        j = (nr[0] == '-' ? 1 : 0);
        while (nr[j] >= '0' && nr[j] <= '9')
            a[i] = a[i]*10 + nr[j++] - '0';
        a[i] *= (nr[0] == '-' ? -1 : 1);
    }*/
    
    for (i = 1; i <= n; i++)
        fscanf(in, "%d", &a[i]);
    
    for (i = 1; i <= n; i++)
    {   
        while(first <= last && a[i] <= a[deque[last]])
            last--;
        deque[++last] = i;

        if (i - deque[first] == k)
            first++;

        if (i >= k)
        {
            if (baza < a[deque[first]])
            {
                baza = a[deque[first]];
                index = deque[first];
            }
        }
    }
    
    while (a[index-1] > baza)
        index--;
    fprintf(out, "%d  %d  %d\n", index, index + k-1, baza);
}