Pagini recente » Cod sursa (job #1543433) | Cod sursa (job #574109) | Cod sursa (job #151397) | Clasament testt9847 | Cod sursa (job #2620618)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, v[100100], poz[100100], a[100100], len_max, sol[100100];
int bin_search(int x) { /// cautam cea mai din stanga pozitie care are valoarea >= cu x
int st = 1, dr = len_max;
while (st <= dr) {
int mij = (st + dr) / 2;
if (x > a[mij])
st = mij + 1;
else
dr = mij - 1;
}
return st;
}
int main() {
fin >> n;
for (int i = 1; i <= n; i++)
fin >> v[i];
for (int i = 1; i <= n; i++) {
int p = bin_search(v[i]);
if (p > len_max)
len_max++;
poz[i] = p; /// retinem lungimea maxima pana la fiecare pozitie
a[p] = v[i];
}
int k = len_max;
for (int i = n; i >= 1; i--)
if (poz[i] == k && (k == len_max || sol[k + 1] > v[i]))
sol[k--] = v[i];
fout << len_max << '\n';
for (int i = 1; i <= len_max; i++)
fout << sol[i] << ' ';
return 0;
}