Pagini recente » Cod sursa (job #2222402) | Cod sursa (job #1064060)
#include <vector>
#include <list>
#include <fstream>
struct Value {
int myVal;
Value *parent;
Value() : myVal(), parent(NULL) {
}
Value(int a) : myVal(a), parent(NULL) {
}
~Value() {
myVal = 0;
parent = NULL;
}
};
int bsearch(std::vector<Value*> &myV, int l, int r, Value *c);
int main() {
std::ifstream in("scmax.in");
std::ofstream out("scmax.out");
int nV, x;
in >> nV;
std::vector<Value*> myV;
std::list<Value*> myL;
in >> x;
myV.push_back(new Value(x));
for(int i = 1; i < nV; i++) {
in >> x;
Value *c = new Value(x);
myL.push_back(c);
unsigned j = bsearch(myV, 0, (int)myV.size() - 1, c);
if(j == 0) {
myV[j] = c;
} else if(j == myV.size()) {
myV.push_back(c);
myV[j]->parent = myV[j - 1];
} else {
myV[j] = c;
myV[j]->parent = myV[j - 1];
}
}
out << myV.size() << '\n';
std::list<int> mySol;
Value *crawl = myV.back();
while(crawl != NULL) {
mySol.push_front(crawl->myVal);
crawl = crawl->parent;
}
for(std::list<int>::iterator it = mySol.begin(); it != mySol.end(); it++) {
out << *it << ' ';
}
for(unsigned i = 0; i < myV.size(); i++) {
myV[i] = NULL;
}
while(!myL.empty()) {
Value *d = myL.front();
delete(d);
myL.pop_front();
}
return 0;
}
int bsearch(std::vector<Value*> &myV, int l, int r, Value *c) {
if(l > r) {
return l;
}
int mid = (r + l) / 2;
if(myV[mid]->myVal >= c->myVal) {
return bsearch(myV, l, mid - 1, c);
}
return bsearch(myV, mid + 1, r, c);
}