Cod sursa(job #1053525)

Utilizator sebinechitasebi nechita sebinechita Data 12 decembrie 2013 20:06:33
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.93 kb
//nu ii frumos sa te uiti pe sursele altora:))...hotule....nu o sti face singur?:))
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
FILE *fin=fopen("scmax.in", "r");
ofstream fout("scmax.out");
#define MAX 100005

const unsigned maxb=8192;
char buf[maxb];
unsigned ptr=maxb;

inline int getInt(){
    int nr=0,mul=1;
    while(buf[ptr]<'0'||'9'<buf[ptr]||buf[ptr]=='-'){
        if(buf[ptr]=='-'){
            mul=-1;
        }
        if(++ptr>=maxb)
            fread(buf,maxb,1,fin),ptr=0;
    }
    while('0'<=buf[ptr]&&buf[ptr]<='9'){
        nr=nr*10+buf[ptr]-'0';
        if(++ptr>=maxb)
            fread(buf,maxb,1,fin),ptr=0;
    }
    return nr*mul;
}

int AIB[MAX], n, v[MAX], rez[MAX], cpy[MAX],up[MAX];
int a[MAX];

int cauta(int p)
{
    int m=0;
    while(p)
    {
        if(v[AIB[p]]>v[m])
            m=AIB[p];
        p-=(p&(p-1)^p);
    }
    return m;
}

void introdu(int p, int val)
{
    while(p<=n)
    {
        if(v[val]>v[AIB[p]])
            AIB[p]=val;
        p+=(p&(p-1)^p);
    }
}

int main()
{
    n=getInt();
    int i;

    for(i=1;i<=n;i++)
    {
        a[i]=getInt();
        cpy[i]=a[i];
    }
    //inc normalizare
    sort(a+1, a+n+1);

    int k=1;
    for(i=2;i<=n;i++)
    {
        if(a[i]!=a[k])
            a[++k]=a[i];
    }

    for(i=1;i<=n;i++)
    {
        v[i]=lower_bound(a+1, a+k+1, cpy[i])-a;
    }
    for(i=1;i<=n;i++)
    {
        a[i]=v[i];
        v[i]=0;
    }
    //sf normalizare
    int maxi=0;

    for(i=1;i<=n;i++)
    {
        up[i]=cauta(a[i]-1);
        //cout<<up[i]<<" ";
        v[i]=v[up[i]]+1;
        introdu(a[i], i);
        if(v[i]>v[maxi])
            maxi=i;
    }
    fout<<v[maxi]<<"\n";
    int g=0;

    for(i=maxi; i ; i=up[i])
    {
        rez[++g]=cpy[i];
        maxi=i;
    }
    for(i=g;i>=1;i--)
    {
        fout<<rez[i]<<" ";
    }

}