Cod sursa(job #750682)

Utilizator test1Trying Here test1 Data 22 mai 2012 19:56:44
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <stdio.h>
int x[100001],n,k,p,u;
struct comp{
    int x,p;
    struct comp*urm; }*a=NULL,*b;

void solution(){
    int pos=1,max=0,nr=0;
    comp*c,*d;
    while(pos<=n-max+1){
        if(a==NULL){
            a=new comp; a->x=x[pos]; a->p=pos; a->urm=NULL; b=a;
            nr=1; }
        while(pos<=n&&(nr<k||nr==k&&x[pos]<=a->x)){
            if(x[pos]>a->x){
                nr++;
                c=new comp; c->x=x[pos]; c->p=pos; c->urm=NULL; b->urm=c; b=c; }
            pos++; }
        if(pos-a->p>max){
            max=pos-a->p;
            p=a->p; u=pos-1; }
        c=a; a=a->urm; delete c; nr--;
        d=a; c=d->urm;
        while(c!=NULL){
            if(c->x<=a->x){
            if(c->urm!=NULL)d->urm=c->urm; else d->urm=NULL; nr--; }
            delete c;
            d=d->urm; c=d->urm; }
    }
}

int main(){
    int i;
    freopen("ksecv.in","r",stdin);
    freopen("ksecv.out","w",stdout);
        scanf("%d %d",&n,&k);
    for(i=1;i<=n;i++)scanf("%d",&x[i]);
    solution();
        printf("%d %d\n",p,u);
}