Cod sursa(job #1462808)

Utilizator om6gaLungu Adrian om6ga Data 19 iulie 2015 01:43:12
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <stdio.h>
#include <string.h>

#define nmax 500005
#define SIZE 3500005

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

int main()
{
    FILE *in, *out;
    in  = fopen("secventa.in", "r");
    out = fopen("secventa.out", "w");
    
    fscanf(in, "%d %d\n", &n, &k);
    
    fgets(string, SIZE, in);
    
    i = 1;
    aux = 0;
    while (string[aux])
    {
        j = aux;
        if (string[aux] == '-')
            aux++;
        while (string[aux] >= '0' && string[aux] <= '9')
            a[i] = a[i]*10 + string[aux++] - '0';
        a[i++] *= (j == '-' ? -1 : 1);
        
        if (string[aux] == ' ')
            aux++;
    }
    
    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]];
                ind = deque[first];
            }
        }
    }
    
    while (ind-1>0 && a[ind-1] >= baza)
        ind--;
    fprintf(out, "%d  %d  %d\n", ind, ind + k-1, baza);
}