Pagini recente » Cod sursa (job #2280793) | Cod sursa (job #2207170) | Cod sursa (job #2905736) | Cod sursa (job #2024841) | Cod sursa (job #1517983)
#include<fstream>
#include<algorithm>
#define NMax 100005
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int N,bst,Res[NMax],v[NMax],lst[NMax],D[NMax],AIB[NMax],up[NMax];
inline void update(int x, int ind)
{ // for(;x<=N;x+=x&-x)
for(;x<=N;x+=(x^(x-1))&x)
if(D[ind]>D[AIB[x]]) AIB[x]=ind;
}
inline int query(int x)
{ int mx=0;
for(;x;x-=(x^(x-1))&x)
if(D[AIB[x]]>D[mx]) mx=AIB[x];
return mx;
}
struct cmp
{ bool operator()(const int &a, const int &b) const
{
return (a<=b);
}
};
int main()
{ int i,h=1;
f >> N;//scanf("%d",&N);
for(i=1;i<=N;++i) {f >> v[i];Res[i]=lst[i]=v[i];}
sort(lst+1,lst+N+1,cmp());
for(i=2;i<=N;++i)
if(lst[i]!=lst[h])
lst[++h]=lst[i];
for(i=1; i<=N; ++i)
v[i]=lower_bound(lst+1,lst+h+1,v[i])-lst;
for(i=1;i<=N;++i)
{ up[i]=query(v[i]-1);
D[i]=D[up[i]]+1;
update(v[i],i);
}
for(i=1;i<=N;++i)
if(D[bst]<D[i]) bst = i;
g << D[bst] << '\n';//printf("%d\n",D[bst]);
for(h=0,i=bst;i;i=up[i])
lst[++h]=Res[i];
for(i=h;i;--i)
g << lst[i] << " ";//printf("%d ",lst[i]);
g<<'\n';//printf("\n");
g.close();
return 0;
}