Cod sursa(job #1021237)

Utilizator 3nTRoPYiorga dan 3nTRoPY Data 3 noiembrie 2013 15:53:34
Problema Statistici de ordine Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

struct nod
{
   int val;
   nod *after;
	 nod *before;
};

int n ,k;

int main(int argc, char *argv[])
{
 		int a;
 		ifstream fin("sdo.in");
 		fin>>n>>k;
 		int i;
 		fin>>a;
 		nod *p = new nod;
 		nod *ad, *t;
 		p->before=p->after=NULL;
 		p->val=a;
 		for(i=1;i<k;i++)
 		{
			fin>>a;
			ad=new nod;
			ad->val=a;
			if(a<p->val)
			{
			   ad->after=p;
			   p->before=ad;
			   ad->before=NULL;
			   p=ad;
		  }
		  else
		  {
			 		t=p;
			 		while(t->after&&t->after->val<a)
			 		   t=t->after;
					   if(t->after)
					   {
			 		      ad->after=t->after;
			 		      ad->before=t;
			 		      t->after=ad;
			 		      t->after->before=ad;
						 }
						 else
						 {
						 		 t->after=ad;
						 		 ad->before=t;
						 		 ad->after=NULL;
						 }
			}
    }
    t=p;
    while(t->after)
       t=t->after;
    nod *u=t;
    for(;i<n;i++)
    {
				fin>>a;
				if(a<u->val)
				{
				   ad=new nod;
				   ad->val=a;
				   ad->before=NULL;
					 ad->after=NULL;
				   if(a<p->val)
				   {
						 ad->after=p;
						 p->before=ad;
						 ad->before=NULL;
						 p=ad;
			     }
			     else
			     {
					 		 t=p;
					 		 while(t->after->val<a)
					 		    t=t->after;
		 		       ad->after=t->after;
		 		       ad->before=t;
		 		       t->after->before=ad;
		 		       t->after=ad;
			     }
           u=u->before;   
           u->after=NULL; 
			  }
    }
    ofstream fout("sdo.out");
    fout<<u->val;
    return EXIT_SUCCESS;
}