Pagini recente » Cod sursa (job #1471899) | Cod sursa (job #2041464)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
///Solutie O(nlogn)
const int NMAX=100001;
int a[NMAX],st[NMAX],poz[NMAX],n,top,sol[NMAX],lug;
inline int CB(int x)
{
int stg=1,drp=top,mij,poz=1;
while(stg<=drp)
{
mij=(stg+drp)/2;
if(st[mij]>=x)
{
poz=mij;
drp=mij-1;
}
else stg=mij+1;
}
return poz;
}
inline void Solve()
{
top=1;
int x,y;
x=a[top];
st[top]=x;
poz[top]=1;
for(int i=2; i<=n; i++)
{
x=a[i];
if(x<=st[1])
{
st[1]=x;
poz[i]=1;
}
else if(x>st[top])
{
st[++top]=x;
poz[i]=top;
}
else
{
y=CB(x);
st[y]=x;
poz[i]=y;
}
}
int mx=1;
for(int i=1; i<=n; i++)
mx=max(mx,poz[i]);
fout<<mx<<"\n";
for(int i=n; i>=1 && mx; i--)
if(poz[i]==mx)
{
++lug;
sol[lug]=a[i];
mx--;
}
for(int i=lug; i>=1; i--)
fout<<sol[i]<<" ";
}
int main()
{
fin>>n;
for(int i=1; i<=n; i++)
fin>>a[i];
Solve();
fin.close();
fout.close();
return 0;
}