Cod sursa(job #607453)

Utilizator SteveStefan Eniceicu Steve Data 12 august 2011 03:03:11
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream.h>

typedef long miu;

typedef struct numar
{
	miu nr;
	miu pozitie;
	numar *pprev, *pnext;
};

numar *f=NULL, *l=NULL;
miu max=-30001;
numar element;
miu pozitieprev=0;

void push_front (miu a, miu j)
{
	numar *plocal=new numar;
	plocal->nr=a;
	plocal->pozitie=j;
	plocal->pprev=l;
	plocal->pnext=NULL;
	l=plocal;
	if (f == NULL) f=plocal;
	else
	{
		if (l->pprev != NULL) l->pprev->pnext=l;
	}
}

void push_back (miu a, miu j)
{
	numar *plocal=new numar;
	plocal->nr=a;
	plocal->pozitie=j;
	plocal->pprev=NULL;
	plocal->pnext=f;
	f=plocal;
	if (l == NULL) l=plocal;
	else
	{
		if (f->pnext != NULL) f->pnext->pprev=f;
	}
}

void pop_back ()
{
	memcpy (&element, f, sizeof (numar));
	if (l == f) l=NULL;
	delete f;
	/*if (element.pnext == NULL) l=NULL;
	else
	{
		f=element.pnext;
		f->pprev=NULL;
	}*/
	f=element.pnext;
}

int main ()
{
	miu N, K, i, b, spart, unde;
	numar *pmain;
	ifstream fin;
	fin.open ("secventa.in");
	fin>>N>>K;
	for (i=0; i<N; i++)
	{
		spart=0;
		fin>>b;
		pmain=f;
		while (pmain != NULL)
		{
			if (pmain->nr >= b)
			{
				pop_back ();
				pmain=f;
				spart=1;
			}
			else break;
		}
		if (spart) push_back (b, i);
		else push_front (b, i);
		if (f->pozitie <= i-K)
		{
			if (f->nr >= max)
			{
				max=f->nr;
				unde=f->pozitie;
			}
			pop_back ();
		}
	}
	ofstream fout;
	fout.open ("secventa.out");
	if (f->nr > max)
	{
		fout<<f->pozitie+1<<" "<<N<<" "<<f->nr;
		fout.close ();
		return 0;
	}
	if (unde-K+1 > 0) fout<<unde-K+2<<" "<<unde+1;
	else fout<<1<<" "<<K;
	fout<<" "<<max;
	fout.close ();
	return 0;
}