Cod sursa(job #1770051)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 3 octombrie 2016 18:32:54
Problema Subsir crescator maximal Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
const int maxn = 100005;
int w[maxn];
int v[maxn];
int nr;

int cautbin(int x)
{
    int st = 1;
    int dr = nr;
    int ret = 0;
    while(st <= dr)
    {
        int mij = (st + dr) / 2;
        if(w[mij] > x)
        {
            ret = mij;
            dr = mij - 1;
        }
        else
            st = mij + 1;
    }
    return ret;
}

int main()
{
    int n;
    in >> n;
    for(int i = 1; i <= n; i++)
        in >> v[i];
    for(int i = 1; i <= n; i++)
    {
        if(v[i] > w[nr])
            w[++nr] = v[i];
        else if(v[i] < w[nr])
        {
            int poz = cautbin(v[i]);
            w[poz] = v[i];
        }
    }
    out << nr << "\n";
    for(int i = 1; i <= nr; i++)
        out << w[i] << " ";
    out << "\n";
    return 0;
}