Pagini recente » Cod sursa (job #159838) | Cod sursa (job #432519) | Cod sursa (job #3218968) | Cod sursa (job #2635969) | Cod sursa (job #860616)
Cod sursa(job #860616)
#include <fstream>
using namespace std;
ifstream in ("scmax.in");
ofstream out ("scmax.out");
#define DIM 100001
int N, V[DIM], Q[DIM], P[DIM], length, ind[DIM], contor;
int bs (int nr)
{
int lo, hi, mid, pos = 0;
lo = 1, hi = length;
while (lo <= hi)
{
mid = (lo + hi)/2;
if (nr <= Q[mid]) pos = mid, hi = mid - 1;
else lo = mid + 1;
}
if (pos == 0) return ++ length;
else return pos;
}
int main()
{
int i;
in >> N;
for (i = 1; i <= N; i++)
{
in >> V[i];
}
length = 1;
Q[1] = V[1], P[1] = length;
for (i = 2; i <= N; i++)
{
int pos = bs(V[i]);
Q[pos] = V[i], P[i] = pos;
}
out << length << '\n';
for (i = N; i >= 1; i--)
{
if (P[i] == length) ind[++contor] = i, length --;
}
for (i = contor; i >= 1; i--)
{
out << V[ind[i]] << " ";
}
return 0;
}