Cod sursa(job #1369569)

Utilizator tudor_bonifateTudor Bonifate tudor_bonifate Data 3 martie 2015 09:47:17
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
long long a[100001],poz[100001],st[100001],n,i,nr,Max,j,k,x,aux;
ifstream f("scmax.in");
ofstream g("scmax.out");
int cautare_binara(int a)
{
    int s=1;
    int d=nr;
    sort(st+1,st+nr+1);
    bool OK=true;
    while (s<=d && OK==true)
    {
        if (st[(s+d)/2]==(a+1)) OK=false;
        else
        {
            if (st[(s+d)/2]>(a+1)) s++;
            else d--;
        }
    }
    if (OK==true) return (s+d)/2;
    else return s;
}
int main()
{
    f>>n;
    for (i=1; i<=n; i++) f>>a[i];
    poz[1]=1;
    st[1]=a[1];
    nr=1;
    for (i=2; i<=n; i++)
    {
        if (a[i]>st[nr])
        {
            st[++nr]=a[i];
            poz[i]=(nr+1);
        }
        else
        {
            x=cautare_binara(a[i]);
            st[x]=a[i];
            poz[i]=x;
        }
    }
    nr=0;
    for (i=1; i<=n && st[i]!=0; i++)
    {
        nr++;
    }
    g<<nr<<'\n';
    for (i=1; i<=n && st[i]!=0; i++)
    {
       g<<st[i]<<' ';
    }
    return 0;
}