Pagini recente » Statistici Ana Ivan (bluemoonai) | Cod sursa (job #2783530)
#include <fstream>
#include <stack>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
int n, m;
int vec[100001], pos[100001], ins_pos[100001];
/// Return the position of the
/// greatest number smaller than the given `val`
int binary_search(int start, int end, int val) {
if (start == end) return start;
int mid = (start + end) / 2;
if (pos[mid] == val) return mid;
if (pos[mid] > val) {
return binary_search(start, mid, val);
}
return binary_search(mid + 1, end, val);
}
void read() {
in >> n;
for(int i = 0; i < n; ++i) {
in >> vec[i];
}
}
void solve() {
pos[0] = vec[0];
m = 1;
for(int i = 1; i < n; ++i) {
int bs = binary_search(0, m, vec[i]);
pos[bs] = vec[i];
ins_pos[i] = bs;
if (bs == m && vec[i] > pos[m - 1])
++m;
}
}
void print() {
stack<int> res;
out << m << '\n';
--m;
--n;
while(n >= 0) {
if(ins_pos[n] == m) {
res.push(vec[n]);
--m;
}
--n;
}
while(!res.empty()) {
out << res.top() << ' ';
res.pop();
}
}
int main() {
read();
solve();
print();
return 0;
}