Cod sursa(job #2944051)

Utilizator matei.tudoseMatei Tudose matei.tudose Data 21 noiembrie 2022 23:08:42
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
using namespace std;

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

int n, before[100005], numbers[100005], BIT[100005];

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

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

int query(int &poz)
{
    const int reper = poz + 1;
    int ans = 0, i;
    for (i = poz; i > 0; i -= get_bit(i))
        if (BIT[i] > ans && numbers[i] < numbers[reper])
        {
            ans = BIT[i];
            poz = i;
        }
    return ans;
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> numbers[i];
    for (int i = 1; i <= n; i++)
    {
        if (i == 1)
        {
            update(i, 1);
            before[i] = -1;
            continue;
        }
        int poz = i - 1;
        int ans = query(poz);
        before[i] = ans == 0 ? -1 : poz;
        update(i, ans + 1);
    }
    int poz = n;
    numbers[poz+1] = INT32_MAX;
    int ans = query(poz);
    fout << ans << '\n';
    int i = 0;
    int v[ans];
    while (poz != -1)
    {
        v[i++] = numbers[poz];
        poz = before[poz];
    }
    for (int i = ans - 1; i >= 0; i--)
        fout << v[i] << ' ';
    return 0;
}