Pagini recente » Cod sursa (job #3214207) | Rating Niculescu Teodor Vicentiu (niculescuteodor) | Cod sursa (job #3217451) | Cod sursa (job #2823597) | Cod sursa (job #795206)
Cod sursa(job #795206)
#include <stdio.h>
#include <algorithm>
#define pii pair <int,int>
#define x first
#define y second
#define NMAX 100005
using namespace std;
int n,A[NMAX],C[NMAX],aib[NMAX],Vmax,best[NMAX],rez,rez_poz;
pii B[NMAX];
void read()
{
scanf("%d",&n);
int i;
for (i=1; i<=n; i++)
{
scanf("%d",&A[i]);
B[i].x=A[i]; B[i].y=i;
}
}
void normal()
{
int i;
sort(B+1,B+n+1);
C[B[1].y]=1;
for (i=2; i<=n; i++)
if (B[i].x==B[i-1].x)
C[B[i].y]=C[B[i-1].y];
else
C[B[i].y]=C[B[i-1].y]+1;
Vmax=C[B[n].y];
}
inline int max(int x,int y)
{
return x>y ? x : y;
}
int lsb(int x)
{
return x & -x;
}
int find_max(int poz)
{
int i,s=0;
for (i=poz; i>0; i-=lsb(i))
s=max(s,aib[i]);
return s;
}
void update(int poz,int val)
{
int i;
for (i=poz; i<=Vmax; i++)
aib[i]=max(aib[i],val);
}
void solve()
{
int i;
for (i=1; i<=n; i++)
{
best[i]=find_max(C[i]-1)+1;
update(C[i],best[i]);
if (best[i]>rez)
rez=best[i],rez_poz=i;
}
printf("%d\n",rez);
}
void recons(int poz,int left)
{
if (left==0)
return ;
int i;
for (i=poz-1; i ; i--)
if (best[i]==left-1 && C[i]<C[poz])
{
recons(i,left-1);
break ;
}
printf("%d ",A[poz]);
}
int main()
{
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
read();
normal();
solve();
recons(rez_poz,rez);
printf("\n");
return 0;
}