Cod sursa(job #1649035)

Utilizator raduzxstefanescu radu raduzx Data 11 martie 2016 12:22:37
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include <fstream>

using namespace std;
typedef int var;
var maxim,v[100005],y[100005],poz[100005],lun[100005],u[1000005];
var cautbin(int a,int n)
{
    int poz1,poz2,i,poz3;
    for(maxim=1;maxim<=n;maxim*=2);
    for(i=maxim/2;i;i/=2)
    {
        if(poz1+i<=n and a<y[poz1+i])
            poz1+=i;
    }
    if(y[poz1]<=a)
        return -1;
    i=poz1;
    poz2=poz1;
    maxim=-1;
    while(y[i]==y[poz1])
    {
        if(maxim<lun[i])
        {
            maxim=lun[i];
            poz2=i;
        }
        i-=1;
    }
    i=poz1;
    poz3=poz1;
    maxim=-1;
    while(y[i]==y[poz1])
    {
        if(maxim<lun[i])
        {
            maxim=lun[i];
            poz3=i;
        }
        i+=1;
    }
    if(lun[poz3]>lun[poz2])
        return poz3;
    return poz2;

}
int main()
{
    ifstream f("scmax.in");
    ofstream g("scmax.out");
    int lungpoz=0,a,n,i,j,k;
    f>>n;
    for(i=1;i<=n;i++)
    {
        f>>v[i];
    }
    lungpoz=1;
    y[1]=v[n];
    poz[n]=0;
    lun[1]=1;
    u[1]=n;
    for(j=n-1;j>=1;j--)
    {
        a=cautbin(v[j],lungpoz);
        if(a==-1)
        {
            lungpoz+=1;
            y[lungpoz]=v[j];
            poz[j]=0;
            lun[lungpoz]=1;
            u[lungpoz]=j;
        }
        else
        {

            poz[j]=u[a];
            u[a]=j;
            lun[a]+=1;
            y[a]=v[j];

        }
    }
    maxim=0;
    var poz1=0;
    for(i=1;i<=lungpoz;i++)
    {
        if(maxim<lun[i])
        {
            maxim=lun[i];
            poz1=u[i];
        }
    }
    g<<maxim<<'\n';
    while(poz[poz1]!=0)
    {
        g<<v[poz1]<<" ";
        poz1=poz[poz1];
    }
    g<<v[poz1]<<" ";
    f.close();
    g.close();
    return 0;
}