Pagini recente » Cod sursa (job #147224) | Cod sursa (job #2686450) | Cod sursa (job #1787600) | Cod sursa (job #2252113) | Cod sursa (job #1990766)
#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;
}