Pagini recente » Cod sursa (job #457267) | Cod sursa (job #1941116) | Cod sursa (job #3177240) | Cod sursa (job #1319777) | Cod sursa (job #2602926)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int N;
int A[100005];
int D[100005];
int cautare_binara (int st,int dr,int elem)
{
while (st<=dr)
{
int mij=st+(dr-st)/2;
if (D[mij]>elem)
{
dr=mij-1;
}
else
{
st=mij+1;
}
}
return st;
}
int main()
{
fin >> N;
int i;
for (i=1;i<=N;++i)
{
fin >> A[i];
}
int k=1;
D[k]=A[1];
for (i=2;i<=N;++i)
{
if (A[i]>D[k])
{
++k;
D[k]=A[i];
}
else
{
int poz=cautare_binara(1,k,A[i]);
D[poz]=A[i];
}
}
fout << k << '\n';
for (i=1;i<=k;++i)
{
fout << D[i] << " ";
}
fin.close();
fout.close();
return 0;
}