Pagini recente » Cod sursa (job #220802) | Cod sursa (job #2567761) | Cod sursa (job #1901904) | Cod sursa (job #2082321) | Cod sursa (job #2855713)
#include <iostream>
#include <fstream>
#include <algorithm>
#define MAXN 100001
#define zeroes(e) ((e^(e-1)) & e)
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int n,v[MAXN],aib[4*MAXN],besti[MAXN],r[MAXN],l[MAXN],maxt,rvalue,h=1,from[MAXN],maxl,afis[MAXN],cur;
void update (int x,int ind)
{
for (; x<=n; x+=zeroes(x))
{
if (besti[ind]>besti[aib[x]])
{
aib[x]=ind;
}
}
}
int query (int x)
{
int max1=0;
for (; x; x-=zeroes(x))
{
if (besti[aib[x]]>besti[max1])
max1=aib[x];
}
return max1;
}
int main()
{
f>>n;
for (int i=1; i<=n; i++)
{
f>>v[i];
r[i]=l[i]=v[i];
}
sort(l+1,l+1+n);
for (int i=2; i<=n; i++)
{
if (l[i]!=l[h])
{
// ++h;
l[++h]=l[i];
//++h;
}
//g<<h<<'\n';
}
for (int i=1; i<=n; i++)
{
v[i]=lower_bound(l+1,l+h+1,v[i])-l;
}
for (int i=1; i<=n; i++)
{
from[i]=query(v[i]-1);
//cout<<from[i]<<' ';
besti[i]=besti[from[i]]+1;
// cout<<besti[i]<<' '<<from[i]<<'\n';
update(v[i],i);
}
// cout<<'\n';
for (int i=1; i<=n; i++)
{
if (besti[i]>besti[maxl])
{
maxl=i;
}
// cout<<besti[i]<<' ';
}
// cout<<'\n';
//cout<<besti[maxl]<<'\n';
cur=maxl;
h=0;
for (int i=maxl; i; i=from[i])
{
afis[h]=r[i];
h++;
}
g<<besti[maxl]<<'\n';
for (int i=h-1; i>=0; i--)
{
g<<afis[i]<<' ';
}
return 0;
}