Pagini recente » Cod sursa (job #4596) | Cod sursa (job #1675169)
#include <iostream>
#include <fstream>
using namespace std;
int a[100002],n,b[100002];
int c[100002];
int lmax,p1,p2,mij;
int main()
{
ifstream f("scmax.in");
ofstream g("scmax.out");
f>>n;
for(int i=1;i<=n;++i)
{
f>>a[i];
}
lmax=0;
for(int 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(p1>lmax)
{
++lmax;
b[lmax]=a[i];
c[i]=lmax;
}
else
{
c[i]=p1;
if(b[p1]<a[i])
{
b[p1]=a[i];
}
}
}
g<<lmax<<"\n";
int k=0;
a[0]=0;
for(int i=1;i<=n;++i)
{
if(c[i]==lmax && a[k]<a[i])
{
k=i;
g<<a[i]<<' ';
lmax--;
}
}
return 0;
}