Pagini recente » Cod sursa (job #2589387) | Cod sursa (job #2591860)
#include <iostream>
using namespace std;
int A[100002],D[100002],P[100002];
int main()
{
int n,k,cnt,cnt1=1;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>A[i];
}
k = 1, D[k] = A[1], P[1] = 1;
for(int i = 2 ; i <= n ; i ++)
{
if(A[i] > D[k])
k++, P[i] = k, D[k]=A[i];
else if(A[i]==D[k])
P[i]=k;
else
{
int st = 1, dr = k, poz = k + 1;
while(st <= dr)
{
int m = (st + dr) / 2;
if(D[m] > A[i])
poz = m, dr = m - 1;
else
st = m + 1;
}
if(A[i]==D[poz])
{
poz--;
}
D[poz] = A[i];
P[i]=poz;
}
}
cnt=k;
for(int i=n;i>=1;i--)
{
if(P[i]==cnt){
cnt--;
D[cnt1]=i;
cnt1++;
}
}
cout<<k<<'\n';
for(int i=cnt1-1;i>=1;i--)
{
cout<<A[D[i]]<<" ";
}
return 0;
}