Pagini recente » Cod sursa (job #3203938) | Cod sursa (job #2707827) | Cod sursa (job #103829) | Cod sursa (job #1485396) | Cod sursa (job #2611425)
#include <bits/stdc++.h>
using namespace std;
#define N 100005
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
int a[N];
int aux[N];
int cur[N];
int ant[N];
int l = 0;
int n;
int cauta (int x, int lun)
{
int st, dr;
st= 1;
dr = lun;
while (st < dr)
{
int mij = (st + dr) / 2;
if (x > aux[mij])
st = mij + 1;
else
dr = mij;
}
return st;
}
void tipar (int poz)
{
if (poz == 0)
return;
else
{
tipar (ant[poz]);
fout << a[poz] << " ";
}
}
int main ()
{
fin >> n;
for (int i = 1; i <= n; i++)
fin >> a[i];
aux[1] = a[1];
cur[1] = 1;
l++;
for (int i = 2; i <= n; i++)
{
int x = cauta (a[i], l);
if (a[i] > aux[x])
{
aux[++l] = a[i];
cur[l] = i;
ant[i] = cur[l - 1];
}
else
{
aux[x] = a[i];
cur[x] = i;
ant[i] = cur[x - 1];
}
}
fout << l << "\n";
tipar (cur[l]);
}