Cod sursa(job #1058572)

Utilizator 3nTRoPYiorga dan 3nTRoPY Data 15 decembrie 2013 17:59:41
Problema Deque Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>
#include <iostream>

using namespace std;

struct nod
{
 			 long val;
 			 int in;
 			 nod *next;
 			 nod *before;
}*p;

void push(int x, int i)
{
 		 nod *ad=new nod;
 		 ad->val=x;
 		 ad->in=i;
 		 ad->next=NULL;
 		 if(p==p->next)
 		 {
 		      p->next=ad;
 		      p->before=ad;
 		      ad->next=p;
 		      ad->before=p;
     }
     else
     {
		 		 ad->next=p;
		 		 ad->before=p->before;
		 		 p->before->next=ad;
		 		 p->before=ad;
     }
}

int pop(int poz)
{
 		int ret;
 		nod *t;
 		if(poz==1)
 		{
		 		t=p->next;
				 p->next=t->next;
				 t->next->before=p;
				 ret=t->val;
				 delete t;			
    }
    else
    {
		 		t=p->before;
		 		p->before=t->before;
		 		t->before->next=p;
		 		ret=t->val;
		 		delete t;
    }
    return ret;
}

int main(int argc, char *argv[])
{
 		p=new nod;
 		int a;
 		p->next=p->before=p;
 		ifstream fin("deque.in");
 		ofstream fout("deque.out");
 		int n, k, x, i;
 		long s=0;
 		fin>>n>>k;
 		for(i=0;i<n;i++)
 		{
			 fin>>x;
			 while(p!=p->next&&p->before->val>=x)
			    pop(2);
       push(x, i);
       if(p->next->in==i-k)
          pop(1);
       if(i>=k-1)
       {
          a=p->next->val;		 
          s+=a;
          cout<<a<<endl;
			 } 
    }
    fout<<s;
    return EXIT_SUCCESS;
}