Cod sursa(job #2530303)

Utilizator mihaela.macarie01@yahoo.comMihaela Macarie [email protected] Data 24 ianuarie 2020 17:23:05
Problema Schi Scor 55
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream x("schi.in");
ofstream y("schi.out");

int n,i,nr;

struct nod
{
    int poz;
    int info;
    nod *urm,*ant;
};

nod *st=NULL,*dr=NULL;

void post(int nr, nod * &dr, nod *st, int pozi)
{
    nod *p=st;
    int ord=1;
    while(ord<nr-1)
        ord++,p=p->urm;
    nod *t=new nod;
    t->info=nr;
    t->poz=pozi;
    t->urm=p->urm;
    if(p->urm!=NULL)
        p->urm->ant=t;
    else
        dr=t,dr->ant=p;
    t->ant=p;
    p->urm=t;
}

int main()
{
    x>>n;
    x>>nr;
    nod *st=new nod;
    st->poz=1;
    st->info=nr;
    st->urm=NULL;
    st->ant=NULL;
    dr=st;
    for(i=2;i<=n;i++)
    {
        x>>nr;
        if(nr<=st->info)
        {
            nod *t=new nod;
            t->info=nr;
            t->poz=i;
            t->urm=st;
            t->ant=NULL;
            st->ant=t;
            st=t;
        }
        else
            post(nr,dr,st,i);
    }
    nod *p=st;
    while(p!=NULL)
    {
        y<<p->poz<<'\n';
        p=p->urm;
    }
    x.close();
    y.close();
    return 0;
}