Pagini recente » Cod sursa (job #1017566) | Cod sursa (job #3289700) | Cod sursa (job #1326992) | Cod sursa (job #2931880) | Cod sursa (job #2487409)
#include <bits/stdc++.h>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int i,n,v[100005],l[100005],aib[100005],x,Max,p,poz,a[100005],b[100005];
map <int,vector<int>> mp;
stack <int> st;
int ub(int x)
{
return x&(-x);
}
void update(int x, int val)
{
for(int i=x;i<=n;i+=ub(i))
if(aib[i]<val)
aib[i]=val;
}
int query(int x)
{
int s=0;
for(int i=x;i>=1;i-=ub(i))
if(aib[i]>s)
s=aib[i];
return s;
}
void normalizare()
{
for(int i=1;i<=n;i++)
b[i]=a[i];
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)
{
int st=1,p=0;
int dr=n;
int x=a[i];
v[i]=lower_bound(b+1,b+n+1,x)-b;
}
}
int main()
{
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
normalizare();
for(i=1;i<=n;i++)
{
x=query(v[i]-1)+1;
l[i]=x;
if(x>Max)
{
Max=x;
p=i;
}
update(v[i],x);
}
g<<Max<<'\n';
poz=p;
for(i=p-1;i>=1;i--)
{
x=l[i];
if(v[i]<v[poz] && x==Max-1)
{
poz=i;
Max=x;
st.push(a[i]);
}
}
while(!st.empty())
{
g<<st.top()<<' ';
st.pop();
}
g<<a[p]<<'\n';
return 0;
}