Cod sursa(job #3144996)

Utilizator verde.cristian2005Verde Flaviu-Cristian verde.cristian2005 Data 11 august 2023 18:22:58
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>
using namespace std;

ifstream in("scmax.in");
ofstream out("scmax.out");

int lmax[100001], v[100001], venit[100001];
int afis[100001];
int vec[100001];

int aib[100001], n, cnt;

int lsb(int x)
{
    return x & -x;
}

void update(int poz, int val)
{
    for(; poz <= n; poz += lsb(poz))
        aib[poz] = max(aib[poz], val);
}

int query(int poz)
{
    int max1 = 0;
    for(; poz > 0; poz -= lsb(poz))
        max1 = max(max1, aib[poz]);
    return max1;
}

int cautbin(int val)
{
    int r = 0, pas = 1 << 16;
    while(pas)
    {
        if(r + pas <= cnt && vec[r + pas] <= val)
            r += pas;
        pas /= 2;
    }
    return r;
}

int main()
{
    int max1 = 0;
    in >> n;
    for(int i = 1; i <= n; i++)
    {
        in >> v[i];
        vec[i] = v[i];
    }
    sort(vec + 1, vec + n + 1);
    for(int i = 1; i <= n; i++)
        if(vec[i] != vec[i - 1])
            vec[++cnt] = vec[i];
    for(int i = 1; i <= n; i++)
        v[i] = cautbin(v[i]);
    for(int i = 1; i <= n; i++)
    {
        lmax[i] = query(v[i] - 1) + 1;
        update(v[i], lmax[i]);
        max1 = max(max1, lmax[i]);
    }
    out << max1 << '\n';
    int cnt = 0;
    for(int i = n; i >= 1; i--)
        if(lmax[i] == max1)
        {
            afis[++cnt] = vec[v[i]];
            max1--;
        }
    for(int i = cnt; i >= 1; i--)
        out << afis[i] << " ";
    return 0;
}