Cod sursa(job #1990760)

Utilizator cosmo0093Raduta Cosmin cosmo0093 Data 13 iunie 2017 14:44:34
Problema Subsir crescator maximal Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <vector>

struct Pair {
    int val;
    int prev;
};

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

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

    int nV;

    fileIn >> nV;

    std::vector<Pair> myV(nV);

    for (int i(0); i < nV; i++) {
        fileIn >> myV[i].val;
        myV[i].prev = -1;
    }

    std::vector<int> myL;

    myL.push_back(0);

    int aux;
    for (int i(1); i < nV; i++) {
        aux = binary(myL, myV, i, 0, myL.size() - 1);
        if (aux == 0) {
            myL[0] = i;
        } else if (aux == myL.size()) {
            myV[i].prev = myL.back();
            myL.push_back(i);
        } else {
            myV[i].prev = myL[aux - 1];
            myL[aux] = i;
        }
    }

    std::vector<int> res;

    res.push_back(myL.back());
    aux = myV[myL.back()].prev;

    while (aux != -1) {
        res.push_back(aux);
        aux = myV[aux].prev;
    }

    fileOut << res.size() << '\n';
    for (int i : res) {
        fileOut << myV[i].val << ' ';
    }

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

    return 0;
}