Cod sursa(job #615265)

Utilizator skyelHighScore skyel Data 9 octombrie 2011 02:35:58
Problema Secventa Scor 80
Compilator c Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define input "secventa.in"
#define output "secventa.out"
int array[500005], position[500005];

int main()
{
    int n,k,i,begin,end,max,maxp;
    char *it;
    freopen(input, "r",stdin);
    freopen(output, "w",stdout);
    scanf("%d",&n);
    scanf("%d\n",&k);
    for (i = 0; i < n; ++i)
    {
        scanf("%d",&array[i]);
    }
    begin = 1;
    end = 0;
    max = -30001;
    maxp = 0;
    for (i = 0; i < n; ++i)
    {
        while (array[i] <= array[position[end]] && end >= begin) end--;
        while (end >= begin && ((i - position[begin]) >= k)) begin++;
        
        position[++end] = i;
        if(i >= (k-1) && max < array[position[begin]])
        {
            max = array[position[begin]];
            maxp = i;
        }
    }
    maxp++;
    printf("%d %d %d\n",maxp - k + 1, maxp, max);
    return 0;
}