Cod sursa(job #287327)

Utilizator laserbeamBalan Catalin laserbeam Data 24 martie 2009 18:43:30
Problema Secventa Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include<fstream>
using namespace std;
struct node
{
	int val,poz;
	node *next;
};
typedef struct node *lista;

void addnode(lista &first, lista &last, int x, int y)
{
	node *p = new node;
	p->val=x;
	p->poz=y;
	p->next=NULL;
	if (!first)first=p;
	if (last)last->next=p;
	last=p;
}
void deletenode(lista &first)
{
	node *p;
	p=first;
	first=first->next;
	delete p;
}

int main()
{
ifstream f("secventa.in");
long i,n,k,c,max1,dr,st;
dr=st=0;
f>>n>>k;
lista first,last;
first=last=NULL;
for (i=1;i<k;i++)
{
	f>>c;
	addnode(first,last,c,i);
	while (first->val>c)deletenode(first);
}
max1=-30020;
for (i=k;i<=n;i++)
{
	f>>c;
	addnode(first,last,c,i);
	if (first->poz==i-k)deletenode(first);
	while (first->val>c)deletenode(first);
	if (max1<first->val){max1=first->val;dr=i;st=i-k+1;}
	
}
f.close();
ofstream g("secventa.out");
g<<st<<' '<<dr<<' '<<max1<<'\n';
g.close();

return 0;
}