Pagini recente » Cod sursa (job #264252) | Cod sursa (job #694321) | Cod sursa (job #614523) | Cod sursa (job #1057762) | Cod sursa (job #2762922)
#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 find_secv(int c, int l, int r) {
if(l == r)
return l;
int m = (l + r) / 2;
if(seg[2 * c] == val - 1)
return find_secv(2 * c, l, m);
return find_secv(2 * c + 1, m + 1, r);
}
int main() {
int n; fin >> n;
vector<int> a(n);
unordered_map<int, int> coordinate;
unordered_map<int, int> r_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;
r_coordinate[k++] = aux[i];
}
unordered_map<int, int> ans;
int last_ans = 1;
for(int i = 0; i < n; ++i) {
poz = coordinate[a[i]];
val = find_max(1, 1, n) + 1;
int last_poz = find_secv(1, 1, n);
if(last_poz < poz) ans[val - 1] = last_poz;
if(val > seg[1])
last_ans = poz;
// cout << i << ' ' << val << ' ' << find_secv(1, 1, n) << ' ' << seg[1] << '\n';
// for(int j = 1; j <= 35; ++j)
// cout << seg[j] << ' ';
// cout << '\n';
update(1, 1, n);
}
fout << seg[1] << '\n';
ans[seg[1]] = last_ans;
for(int i = 1; i <= seg[1]; ++i)
fout << r_coordinate[ans[i]] << ' ';
return 0;
}