Pagini recente » Cod sursa (job #2268129) | Cod sursa (job #3132641) | Cod sursa (job #543639) | Cod sursa (job #202101) | Cod sursa (job #1097780)
/* Subsir crescator maximal folosind cautare binara + o coada */
#include <stdio.h>
#define MAX 100001
int N, V[MAX], Bst[MAX], C[MAX], K = 0;
int CautBin(int X)
{
int Left = 1, Right = K + 1, Middle;
while (Right - Left > 1)
{
Middle = (Left + Right) / 2;
if (C[Middle] <= X) Left = Middle; else Right = Middle;
}
if (C[Left] < X) Left = Right;
if (Left > K) K = Left;
C[Left] = X;
}
void Afis(int N)
{
if (K == 0)
{
return;
}
for (int i = N; i > 0; i--)
{
if (Bst[i] == K)
{
K--;
Afis(N - 1);
printf("%d ", V[i]);
}
}
}
int main()
{
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
scanf("%d", &N);
for (int i = 1; i <= N; i++)
{
scanf("%d", &V[i]);
Bst[i] = CautBin(V[i]);
}
printf("%d\n", K);
Afis(N);
}