Pagini recente » Cod sursa (job #2190459) | Clasament wellcodesimulareclasa10-11martie | Cod sursa (job #2395027) | Cod sursa (job #1662478) | Cod sursa (job #2762920)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int NMAX = 1e5;
vector<int> seg(4 * NMAX + 77, 0);
int val, poz;
void update(int c, int l, int r) {
if(l == r) {
seg[c] = val;
return;
}
int m = (l + r) / 2;
if(poz <= m)
update(2 * c, l, m);
else
update(2 * c + 1, m + 1, r);
seg[c] = max(seg[2 * c], seg[2 * c + 1]);
}
int find_max(int c, int l, int r) {
if(l >= poz)
return 0;
if(r < poz)
return seg[c];
int m = (l + r) / 2;
return max(find_max(2 * c, l, m), find_max(2 * c + 1, m + 1, r));
}
int main() {
int n; fin >> n;
vector<int> a(n);
unordered_map<int, int> coordinate;
for(int &x: a)
fin >> x;
vector<int> aux(a.begin(), a.end());
sort(aux.begin(), aux.end());
for(int i = 0, k = 1; i < n; ++i) {
if(coordinate.count(aux[i]))
continue;
coordinate[aux[i]] = k++;
}
int ans_v = 1, last_prev = -1;
vector<int> dp(n, 1);
vector<int> prev(n, -1);
for(int i = 0; i < n; ++i) {
poz = coordinate[a[i]];
val = find_max(1, 1, n) + 1;
ans_v = max(ans_v, val);
update(1, 1, n);
}
fout << seg[1] << '\n';
vector<int> ans(ans_v);
while(last_prev != -1)
ans[--ans_v] = a[last_prev],
last_prev = prev[last_prev];
for(int x: ans)
fout << x << ' ';
return 0;
}