Cod sursa(job #1248760)

Utilizator diana-t95FMI Tudoreanu Diana Elena diana-t95 Data 25 octombrie 2014 22:18:03
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include<fstream>
using namespace std;
int n, a[100001], b[100001], c[100001];
int gasit[100001], nr;
ofstream g("scmax.out");
void citeste()
{
    int i;
    ifstream f("scmax.in");
    f>>n;
    for (i=1;i<=n;i++)
        f>>a[i];
}
void afisare(int i)
{
    if (gasit[i]==0) g<<a[i]<<' ';
        else
        {
            afisare(gasit[i]);
            g<<a[i]<<' ';
        }
}
void solutie1()
{
    int i, j, maxim=1,pmax;
    b[1]=1;
    gasit[1]=0;
    for (i=2;i<=n;i++)
    {
        j=i-1;
        while (a[i]<a[j] && j>0) j--;
            if (j==0) {b[i]=1; gasit[i]=0;}
                else {b[i]=b[j]+1; if (b[i]>maxim) {maxim=b[i]; pmax=i;} gasit[i]=j;}
    }

    g<<pmax<<'\n';
    afisare(pmax);

}
int cautare(int i, int st, int dr)
{
    int poz=1, pas=1<<21;
    while (pas>>=1)
    {
        if (poz+pas<=nr && a[poz+pas]<=a[i]) poz+=pas;
    }
    if (a[poz]==a[i]) return poz;
    if(poz==nr) {nr++; poz++;}
    return poz;

}
void solutie2()
{
    int i, j=1;
    c[1]=1;
    gasit[1]=0;
    nr=1;
    for (i=2;i<=n;i++)
        {
            j=cautare(i, 1, nr);
            gasit[i]=c[j-1];
            c[j]=i;
        }
    g<<nr<<'\n';
    afisare(nr);
}
int main()
{
    citeste();
    //solutie1();
    solutie2();


}