Pagini recente » Cod sursa (job #990630) | Cod sursa (job #2701649) | Cod sursa (job #2439260) | Cod sursa (job #1224185) | Cod sursa (job #235133)
Cod sursa(job #235133)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
long N, *V;
ifstream in("scmax.in");
in >> N;
V = new long[N];
for (int i = 0; i < N; i++)
in >> V[i];
in.close();
// P holds parent's index
// L[i] holds last index in longest sequence of length i
long *P = new long[N], *L = new long[N], lmax = 0;
for (int i = 0; i < N; i++) {
long number = V[i];
// find position where V[i] should be inserted
// that is find lowest number higher than V[i]
// binary search
long a = 0, b = lmax - 1;
while (a <= b) {
long m = (a + b) / 2;
if (number < V[L[m]])
b = m - 1;
else if (number > V[L[m]])
a = m + 1;
else {
a = m;
b = m - 1;
}
}
// result is at position a
L[a] = i;
if (i > 0)
P[i] = L[i - 1];
else
P[i] = -1;
if (a == lmax)
lmax++;
}
ofstream out("scmax.out");
out << lmax << endl;
for (int i = 0; i < lmax; i++)
out << V[L[i]] << ' ';
out << '\n';
out.close();
return 0;
}