// UVa 481 - What Goes Up
#include <iostream>
#include <vector>
using namespace std;
#define datatype signed long long int
vector<datatype> a, T, R, seq;
datatype find(datatype ini, datatype fin, datatype v) {
if (ini >= fin)
return ini;
datatype mid = (ini + fin) / 2;
if (a[T[mid]] < v)
return find(mid + 1, fin, v);
else
return find(ini, mid, v);
}
int main() {
datatype v;
datatype i = -1;
while (cin >> v) {
a.push_back(v);
i++;
datatype pos = T.size() > 0 ? find(0, T.size() - 1, v) : 0;
if (T.size() > 0 && a[T[pos]] < v)
pos++;
if (pos == 0) {
R.push_back(-1);
if (T.size() == 0)
T.push_back(i);
else if (v < a[T[0]])
T[0] = i;
} else if (pos == T.size()) {
R.push_back(T[pos - 1]);
T.push_back(i);
} else {
T[pos] = i;
R.push_back(T[pos - 1]);
}
}
cout << T.size() << endl;
cout << "-" << endl;
datatype pos = T[T.size() - 1];
while (pos != -1) {
seq.push_back(pos);
pos = R[pos];
}
for (int i = seq.size() - 1; i >= 0; i--) {
cout << a[seq[i]] << endl;
}
return 0;
}
Monday, June 8, 2015
UVa 481 - What Goes Up
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment