Pagini recente » Cod sursa (job #2850883) | Cod sursa (job #1099797) | Cod sursa (job #272818) | Cod sursa (job #1814935) | Cod sursa (job #1673731)
#include<fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int N,a[100009],i,Lmax,p1,p2,mij,j,b[100009],c[100009];
int main()
{
fin>>N;
for(i=1;i<=N;i++)
{
fin>>a[i];
}
Lmax=0;
for(i=N;i>=1;i--)
{
p1=1;
p2=Lmax;
while(p1<=p2)
{
mij=(p1+p2)/2;
if(b[mij]>a[i])
{
p1=mij+1;
}
else
{
p2=mij-1;
}
}
if(p2>=Lmax)
{
Lmax++;
b[Lmax]=a[i];
c[i]=Lmax;
}
else
{
c[i]=p2;
if(b[p2]<a[i])
{
b[p2]=a[i];
}
}
}
fout<<Lmax-1;
fout<<endl;
int k=0;
a[0]=0;
for(i=1;i<=N;i++)
{
if(c[i]==Lmax && a[k]<a[i])
{
k=i;
fout<<a[i]<<" ";
Lmax--;
}
}
fin.close();
fout.close();
return 0;
}