Cod sursa(job #2969480)

Utilizator MihaiZ777MihaiZ MihaiZ777 Data 23 ianuarie 2023 11:30:09
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int n;
int v[10]; //aaaaaaaaaaaaaaa
int bef[1000005];
vector <int> ans;

int top;

int BinSearch(int num) {
    int lf = 1;
    int rg = top;
    int best = 0;

    while (lf <= rg) {
        int mid = (lf + rg) / 2;
        if (v[mid] < num) {
            lf = mid + 1;
            best = mid;
        }
        else {
            rg = mid - 1;
        }
    }
    return best;
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        fin >> x;

        int index = BinSearch(x);
        index++;
        v[index] = x;
        bef[x] = v[index - 1];
        if (index > top) {
            top++;
        }
    }

    fout << top << '\n';

    ans.push_back(v[top]);

    while (bef[v[top]]) {
        ans.push_back(bef[v[top]]);
        top--;
    }

    while (!ans.empty()) {
        fout << ans.back() << ' ';
        ans.pop_back();
    }
    fout << '\n';
}