Pagini recente » Cod sursa (job #611802) | Cod sursa (job #3788) | Cod sursa (job #671902) | Cod sursa (job #1095256) | Cod sursa (job #330722)
Cod sursa(job #330722)
#include<fstream>
#define maxn 100005
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int a[maxn],best[maxn],last[maxn],i,step,n,j,k,ar[maxn],maxt,maxw;
void write_recursive(int x)
{
if(last[x])
write_recursive(last[x]);
g<<a[x]<<" ";
}
int main()
{
f>>n;
for(i=1;i<=n;++i)
f>>a[i];
best[1]=1;
last[1]=0;
ar[1]=1;
maxt=1;
maxw=1;
for(i=2;i<=n;++i)
{
for(step=1;step<=k;step<<=1);
for(j=0;step;step>>=1)
if(j+step<=k&&a[ar[j+step]]<a[i])
j+=step;
ar[j+1]=i;
best[i]=j+1;
last[i]=ar[j];
if(best[i]>maxt)
maxt=best[i],maxw=i;
if(maxt>k)
k=maxt;
}
g<<maxt<<"\n";
write_recursive(maxw);
g<<"\n";
f.close();
g.close();
return 0;
}