Cod sursa(job #1787076)

Utilizator ade_tomiEnache Adelina ade_tomi Data 24 octombrie 2016 09:09:44
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define NMAX 100003
using namespace std;

struct str 
{
    int x, y;

};
str v[NMAX];
int aib[NMAX],s[NMAX], d[NMAX];
bool  cmp (str a, str b)
{
    if (a.x == b.x)
        return a.y > b.y;
     return a.x < b.x;
}
int lsb(int x)
{
    return x & (-x);
}
void update (int poz, int val)
{
    while (poz <= NMAX)
    {
       
        if (d[val]> d[aib[poz]])
            aib[poz] = val;
        poz += lsb(poz);
    }
}
int query(int poz)
{
    int sol = 0;
    while (poz > 0)
    {
        if (d[aib[poz]] > d[sol])
            sol = aib[poz];
        poz -= lsb(poz);
    }
    return sol;
}
int p[NMAX], p_start, n;
int main ()
{
    ifstream cin ("scmax.in");
    ofstream cout ("scmax.out");
    cin >> n;
    int sol = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i].x;
        v[i].y = i;
    }
    sort (v + 1, v + 1 + n, cmp);
    for (int i = 1; i <= n; i++)
    {
        //cout << v[i].x << v[i].y << "\n";
        p[i] = query (v[i].y - 1);

        //cout << p[i] << "\n";
        d[i] = d[p[i]] + 1;
        if (d[i] > sol)
        {
            sol = d[i];
            p_start = i;
        }
        update (v[i].y, i);
    }
    int m = 0;
    cout << sol << "\n";
    while (p_start != 0)
    {
        m ++;
        s[m] = v[p_start].x  ;
        p_start = p[p_start];
    }
    for (int i = m; i >= 1; i--)
        cout << s[i] << " ";
    return 0;

}