Pagini recente » Cod sursa (job #916888) | Cod sursa (job #1500195) | Cod sursa (job #2908658) | Cod sursa (job #2349013) | Cod sursa (job #1349493)
#include <iostream>
#include <vector>
#include <map>
#include <fstream>
using namespace std;
const int maxn = 100005;
int n, a[maxn], uniq, aib[maxn], dp[maxn], father[maxn];
map<int, int> mymap;
inline int lsb(int x) {
return x & (-x);
}
inline void update(int x, int ind) {
for(int i = x ; i <= uniq ; i += lsb(i)) {
if(dp[aib[i]] < dp[ind])
aib[i] = ind;
}
}
inline int query(int x) {
int ind = aib[x];
for(int i = x ; i > 0 ; i -= lsb(i))
if(dp[aib[i]] > dp[ind])
ind = aib[i];
return ind;
}
inline void write(int node, ofstream &fout) {
if(father[node])
write(father[node], fout);
fout << a[node] << ' ';
}
int main() {
ifstream fin("scmax.in");
ofstream fout("scmax.out");
fin >> n;
for(int i = 1 ; i <= n ; ++ i) {
fin >> a[i];
mymap[a[i]];
}
for(map<int, int> :: iterator it = mymap.begin() ; it != mymap.end() ; ++ it)
mymap[it->first] = ++ uniq;
int ans = 1;
for(int i = 1 ; i <= n ; ++ i) {
int best = query(mymap[a[i]] - 1);
dp[i] = dp[best] + 1;
update(mymap[a[i]], i);
father[i] = best;
ans = max(ans, dp[i]);
}
fout << ans << '\n';
for(int i = 1 ; i <= n ; ++ i)
if(dp[i] == ans) {
write(i, fout);
return 0;
}
}