Cod sursa(job #1400440)

Utilizator mateidanutDanut Gabriel Matei mateidanut Data 25 martie 2015 12:04:32
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#define NMAX 100001
using namespace std;

int n, i, x, k, nr, poz, v[NMAX], q[NMAX], t[NMAX], sol[NMAX];

ifstream f("scmax.in");
ofstream g("scmax.out");

void DI(int st, int dr, int x, int &poz)
{   if (st<=dr && !poz)
    {   int m=(st+dr)/2;
        if (q[m]>=x && q[m-1]<x)
            poz=m;
        else
        {   if (q[m]<x)
                DI(m+1, dr, x, poz);
            else
                DI(st, m-1, x, poz);
        }
    }
}

int main()
{   f>>n;
    for (i=1; i<=n; ++i)
        f>>v[i];
    for (i=1; i<=n; ++i)
    {   poz=0;
        DI(1, nr, v[i], poz);
        if (poz)
        {   q[poz]=v[i];
            t[++k]=poz;
        }
        else
        {   q[++nr]=v[i];
            t[++k]=nr;
        }
    }
    g<<nr<<'\n';
    for (i=k, x=nr; i>=1 && x; --i)
        if (t[i]==x)
        {   sol[x]=v[i];
            --x;
        }
    for (i=1; i<=nr; ++i)
        g<<sol[i]<<' ';
    g<<'\n';
    return 0;
}