Cod sursa(job #2696875)

Utilizator Vlad_AnicaAnica-Popa Vlad-Ioan Vlad_Anica Data 17 ianuarie 2021 00:20:24
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <stdio.h>
#include <iostream>

using namespace std;

const int NMAX = 1e5, STEPVALUE = (1 << 16), INFINIT = 2e9 + 1;
int v[NMAX], smax[NMAX], dp[NMAX], sol[NMAX];

int main()
{
    int n, i, step, pos, cntmax = -1, j, ultelem;
    FILE *fin = fopen("scmax.in", "r");
    fscanf(fin, "%d", &n);
    for (i = 0; i < n; i++)
    {
        fscanf(fin, "%d", &v[i]);
        step = STEPVALUE;
        pos = -1;
        while (step > 0)
        {
            if (pos + step <= cntmax && v[smax[pos + step]] < v[i])
                pos += step;
            step >>= 1;
        }
        if (pos == cntmax)
            cntmax++;
        smax[pos + 1] = i;
        dp[i] = pos + 1;
    }
    fclose(fin);
    FILE *fout = fopen("scmax.out", "w");
    fprintf(fout, "%d\n", cntmax + 1);
    i = n - 1;
    j = -1;
    ultelem = INFINIT;
    while (cntmax >= 0)
    {
        if (dp[i] == cntmax && ultelem > v[i])
        {
            ultelem = v[i];
            cntmax--;
            sol[++j] = i;
        }
        i--;
    }

    for (j; j >= 0; j--)
        fprintf(fout, "%d ", v[sol[j]]);
    fclose(fout);
    return 0;
}