Cod sursa(job #2481228)

Utilizator Dumnezeu129081IIsus Hristos Dumnezeu129081 Data 26 octombrie 2019 17:12:33
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#define MAX_DIM 100000

#include <fstream>
using namespace std;

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

int n, a[MAX_DIM + 1], rasp[MAX_DIM + 1];

void Inserare(int x);

int main()
{
    int m, x;

    fin >> m;
    for (int i = 1; i <= m; ++i)
    {
        fin >> x;
        Inserare(x);
    }

    fout << n << '\n';
    for (int i = 1; i <= n; ++i)
    {
        fout << rasp[i] << ' ';
    }

    fin.close();
    fout.close();
    return 0;
}

void Inserare(int x)
{
    if (x == a[n]) { return; }
    if (x > a[n]) { a[++n] = x; rasp[n] = x; return; }

    int st = 1, dr = n, mij, poz;
    while (st <= dr)
    {
        mij = (st + dr) / 2;
        if (x > a[mij]) { dr = mij - 1; }
        else { poz = mij; st = mij + 1; }
    }

    a[poz] = x;
    if (poz == 1) { rasp[poz] = x; }
    if (poz == n) { rasp[poz] = x; }
}