Pagini recente » Cod sursa (job #2649080) | Cod sursa (job #2853468) | Cod sursa (job #2212663) | Cod sursa (job #2570703) | Cod sursa (job #992237)
Cod sursa(job #992237)
#include<fstream>
#include<algorithm>
#define N 100100
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int i,nr,n,poz,maxi,p,v[N],a[N],b[N],aib[N],d[N],pred[N];
inline void afis(int x)
{
if(!x)
{
g<<nr<<'\n';
return ;
}
++nr;
afis(pred[x]);
g<<v[x]<<' ';
}
inline void update(int x,int p)
{
for(;p<=n;p+=(p&(-p)))
if(d[aib[p]]<d[x])
aib[p]=x;
}
inline int query(int x)
{
int maxi=0;
for(;x;x-=(x&(-x)))
if(d[aib[x]]>d[maxi])
maxi=aib[x];
return maxi;
}
inline int cautbin(int v[],int li,int ls,int x)
{
int mij;
while(li<=ls)
{
mij=(li+ls)>>1;
if(v[mij]==x)
return mij;
if(v[mij]>x)
ls=mij-1;
else
li=mij+1;
}
}
int main()
{
f>>n;
for(i=1;i<=n;++i)
{
f>>a[i];
v[i]=b[i]=a[i];
}
sort(b+1,b+n+1);
for(i=1;i<=n;++i)
if(b[i]!=b[nr])
b[++nr]=b[i];
//for(i=1;i<=n;++i)
// a[i]=cautbin(b,1,nr,a[i]);
nr=0;
for(i=1;i<=n;++i)
{
p=query(a[i]-1);
d[i]=d[p]+1;
pred[i]=p;
update(i,a[i]);
}
for(i=1;i<=n;++i)
if(d[i]>maxi)
maxi=d[i],poz=i;
afis(poz);
}