Cod sursa(job #603694)

Utilizator vlad2901Vlad Berindei vlad2901 Data 18 iulie 2011 13:17:18
Problema Secventa Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct list
{
    int info, poz;
    list *urm, *prec;
} *p, *u;

int n ,k;
char s[5000000];


int main()
{
    int x, max = -31000;
    int i, umax = 0;
    list *nod, *aux;
    char *xs;

    freopen("secventa.in", "r", stdin);
    freopen("secventa.out", "w", stdout);

    scanf("%d %d", &n, &k);

    gets(s);
    gets(s);

    xs = strtok(s, " ");
    i = 1;

    while(xs != NULL)
    {
        x = atoi(xs);

        while(u && u->info > x)
        {
            if(u == p)
            {
                p = NULL;
            }

            aux = u->prec;
            delete u;
            u = aux;
        }

        nod = new list;
        nod->info = x;
        nod->poz = i;
        nod->prec = u;
        nod->urm = NULL;

        if(!p)
        {
            p = nod;
        }

        if(u)
        {
            u->urm = nod;
        }
        u = nod;

        if(nod->poz - p->poz >= k)
        {
            aux = p->urm;
            delete p;
            p = aux;
            p->prec = NULL;

            if(i>=k && max < p->info)
            {
                max = p->info;
                umax = u->poz - k + 1;
            }
        }

        xs = strtok(NULL, " ");
        i++;

    }

    printf("%d %d %d", umax, umax + k -1, max);

    return 0;
}