Pagini recente » Cod sursa (job #1651523) | Cod sursa (job #344816) | Cod sursa (job #489572) | Cod sursa (job #338401) | Cod sursa (job #2605264)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int nmax=100005;
int n,a[nmax],lst[nmax],res[nmax],up[nmax],dp[nmax],aib[nmax],best;
void read()
{
fin>>n;
for(int i=1;i<=n;i++)
{
fin>>a[i];
res[i]=lst[i]=a[i];
}
}
void update(int poz,int ind)
{
for(;poz<=n;poz+= poz & -poz)
if(dp[ind]>dp[aib[poz]])
aib[poz]=ind;
}
int query(int poz)
{
int mx=0;
for(;poz>0;poz-= poz & -poz)
if(dp[aib[poz]]>dp[mx])
mx=aib[poz];
return mx;
}
int main()
{
read();
sort(lst+1,lst+n+1);
int h=0;
for(int i=1;i<=n;i++)//elimina elementele care sunt identice si consecutive in vectorul sortat
if(lst[i]!=lst[i+1])
lst[++h]=lst[i];
for(int i=1;i<=n;i++)
a[i]=lower_bound(lst+1,lst+h+1,a[i])-lst;
for(int i=1;i<=n;i++)
{
up[i]=query(a[i]-1);
dp[i]=dp[up[i]]+1;
update(a[i],i);
}
for(int i=1;i<=n;i++)
if(dp[best]<dp[i])
best=i;
fout<<dp[best]<<"\n";
h=0;
for(int i=best;i>0;i=up[i])
lst[++h]=res[i];
for(;h>0;--h)
fout<<lst[h]<<" ";
return 0;
}