Cod sursa(job #1990766)

Utilizator cosmo0093Raduta Cosmin cosmo0093 Data 13 iunie 2017 15:09:39
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <vector>

struct Pair {
    int val;
    Pair* prev;
};

int binary(std::vector<Pair*> &myV, Pair *c, int left, int right) {
    if (left > right) {
        return left;
    }
    int m = (right + left) / 2;
    if (myV[m]->val >= c->val) {
        return binary(myV, c, left, m - 1);
    }
    return binary(myV, c, m + 1, right);
}

int main() {
    std::ifstream fileIn("scmax.in");
    std::ofstream fileOut("scmax.out");

    int nV, pos;

    fileIn >> nV;

    std::vector<Pair*> myV, cleanup;
    std::vector<int> res;

    Pair *aux = new Pair;

    fileIn >> aux->val;
    aux->prev = nullptr;
    myV.push_back(aux);

    cleanup.push_back(aux);

    for (int i(1); i < nV; i++) {
        aux = new Pair;
        fileIn >> aux->val;
        aux->prev = nullptr;

        cleanup.push_back(aux);

        pos = binary(myV, aux, 0, myV.size() - 1);

        if (pos == 0) {
            myV[0] = aux;
        } else if (pos == myV.size()) {
            aux->prev = myV.back();
            myV.push_back(aux);
        } else {
            aux->prev = myV[pos - 1];
            myV[pos] = aux;
        }
    }

    aux = myV.back();

    while (aux != nullptr) {
        res.push_back(aux->val);
        aux = aux->prev;
    }

    fileOut << res.size() << '\n';
    for (auto it = res.rbegin(); it != res.rend(); it++) {
        fileOut << *it << ' ';
    }

    for (Pair *points : cleanup) {
        delete points;
    }

    fileIn.close();
    fileOut.close();

    return 0;
}