Pagini recente » Cod sursa (job #639183) | Cod sursa (job #571082) | Cod sursa (job #793823) | Cod sursa (job #2513161) | Cod sursa (job #1735436)
#include <iostream>
#include <fstream>
using namespace std;
int v[100010];
int st[100010];
int poz[100010];
int caut(int p,int u,int x)
{
int m;
while(p<=u)
{
m=(u+p)/2;
if(x<v[st[m]])
p=m+1;
else
u=m-1;
}
return p;
}
int main()
{
int n,i,sf=0,pst=0;
ifstream in ("scmax.in");
ofstream out ("scmax.out");
in>>n;
for(i=1;i<=n;i++)
in>>v[i];
for(i=n;i>=1;i--)
{
pst=caut(1,sf,v[i]);
if(pst>sf)
{
st[pst]=i;
poz[i]=st[pst-1];
sf=pst;
}
else
{
poz[i]=st[pst-1];
if(v[st[pst]]<v[i])
st[pst]=i;
}
}
out<<sf<<endl;
for(i=st[sf];i>0;i=poz[i])
out<<v[i]<<" ";
return 0;
}