Pagini recente » Cod sursa (job #625620) | Cod sursa (job #1258275) | Cod sursa (job #351360) | Cod sursa (job #2599814) | Cod sursa (job #1674692)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stack>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
int v[100003];
int t[100003];
int s[100003];
int o[100003];
int b[100003];
int aib[100003];
int k;
int n;
int query(int val) {
int M = 0;
for(int i = val; i > 0; i -= i&-i)
if(o[aib[i]] > o[M])
return aib[i];
//M = aib[i];
return M;
}
int update(int val, int ind) {
for(int i = val; i <= n; i += i&-i)
if(o[ind] > o[aib[i]])
aib[i] = ind;
}
int main() {
in >> n;
for(int i = 1; i <= n; i++) {
in >> v[i];
s[i] = v[i];
t[i] = v[i];
}
sort(s+1, s+n+1);
for(int i = 1; i <= n; i++)
v[i] = lower_bound(s, s+n, v[i])-s;
int M = 0;
int MI = 0;
for(int i = 1; i <= n; i++) {
int q = query(v[i]-1);
o[i] = o[q]+1;
//cout << v[i] << " " << q << " " << o[i] << '\n';
b[i] = q;
if(o[i] > M) {
M = o[i];
MI = i;
}
update(v[i], i);
}
stack<int> st;
int P = MI;
while(P != 0) {
st.push(P);
P = b[P];
}
out << M << '\n';
while(!st.empty()) {
out << t[st.top()] << " ";
st.pop();
}
return 0;
}