Pagini recente » Cod sursa (job #3184639) | Cod sursa (job #2381697) | Cod sursa (job #1492035) | Cod sursa (job #1808452) | Cod sursa (job #1346842)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define NMAX 100007
int i,N,f,u,c;
int aib[NMAX],dp[NMAX],t[NMAX],p[NMAX];
vector < int > q;
void write(int pos)
{
if (pos==0) return ;
if (dp[pos]==dp[f]-1 && p[pos]<p[f])
{
f=pos;
write(pos-1);
printf("%d ",t[pos]);
} else write(pos-1);
}
void update(int p,int x)
{
for (;p<=N;p+=(p&(-p)))
aib[p]=max(aib[p],x);
}
int query(int p)
{
int ans=0;
for (;p;p-=(p&(-p)))
ans=max(ans,aib[p]);
return ans;
}
int main()
{
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
scanf("%d",&N);
for (i=1;i<=N;++i)
{
scanf("%d",&t[i]);
q.push_back(t[i]);
}
sort(q.begin(),q.end());
q.resize(distance(q.begin(),unique(q.begin(),q.end())));
for (i=1;i<=N;++i)
{
p[i]=lower_bound(q.begin(),q.end(),t[i])-q.begin()+1;
dp[i]=query(p[i]-1)+1;
update(p[i],dp[i]);
if (dp[i]>c)
{
c=dp[i];
f=i;
}
//printf("%d ",dp[i]);
}
printf("%d\n",dp[f]);
u=f;
write(f);
printf("%d ",t[u]);
return 0;
}