Pagini recente » Cod sursa (job #1044775) | Cod sursa (job #279587) | Cod sursa (job #1147962) | Cod sursa (job #1756325) | Cod sursa (job #832523)
Cod sursa(job #832523)
#include <cstdio>
using namespace std;
char *p;
#define maxn 100005
int v[maxn];
int longest[maxn];
int B[maxn];
int prec[maxn];
int L;
int n;
void scan(int &a)
{
a&=0;
while(*p < '0' || *p > '9')
++p;
while(*p >= '0' && *p <= '9')
a = a * 10 + *(p++)-'0';
}
void citire()
{
long long len;
fseek(stdin,0,SEEK_END);
len = ftell(stdin);
p = new char[len];
rewind(stdin);
fread(p,1,len,stdin);
}
void solve()
{
int logN=1,step,found,i;
for(i=1;i<=n;++i)
{
while((logN << 1) <= L)
logN <<=1;
for(step=logN,found=0;step;step>>=1)
{
if(found+step <= L && v[B[found+step]] < v[i])
found |= step;
}
longest[i] = found+1;
prec[i] = B[found];
if(found == L)
{
++L;
B[L] = i;
}
else if(v[i] < v[B[found+1]])
B[found+1]=i;
}
}
void print(int i)
{
if(i)
{
print(prec[i]);
printf("%d ",v[i]);
}
}
int main()
{
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
int i;
citire();
scan(n);
for(i=1;i<=n;++i)
scan(v[i]);
solve();
for(i=n;i;--i)
if(longest[i] == L)
{
printf("%d\n",L);
print(i);
return 0;
}
return 0;
}