Pagini recente » Cod sursa (job #2418748) | Cod sursa (job #2149824) | Cod sursa (job #1564889) | Cod sursa (job #1564907) | Cod sursa (job #2510791)
#include <algorithm>
#include <fstream>
const int MAX_N = 100005;
std::ifstream fin("scmax.in");
std::ofstream fout("scmax.out");
int n, a[MAX_N];
int AIB[MAX_N], best[MAX_N];
int last[MAX_N], ord[MAX_N], aux[MAX_N];
void update(int pos, int val) {
for (int i = pos; i <= n; i += i & (-i))
if (best[val] > best[AIB[i]])
AIB[i] = val;
}
int query(int pos) {
int max = 0;
for (int i = pos; i > 0; i -= i & (-i))
if (best[AIB[i]] > best[max])
max = AIB[i];
return max;
}
void afis(int pos) {
if (pos != 0) {
afis(last[pos]);
fout << a[pos] << " ";
}
}
int main() {
fin >> n;
for (int i = 1; i <= n; i++) {
fin >> a[i];
aux[i] = a[i];
}
std::sort(aux + 1, aux + n + 1);
int nr = 1;
for (int i = 2; i <= n; i++)
if (aux[i] != aux[nr])
aux[++nr] = aux[i];
for (int i = 1; i <= n; i++)
ord[i] = std::lower_bound(aux + 1, aux + nr + 1, a[i]) - aux;
for (int i = 1; i <= n; i++) {
last[i] = query(ord[i] - 1);
best[i] = best[last[i]] + 1;
update(ord[i], i);
}
int sol = 0;
for (int i = 1; i <= n; i++)
if (best[sol] < best[i])
sol = i;
fout << best[sol] << '\n';
afis(sol);
return 0;
}