Pagini recente » Cod sursa (job #2563394) | Cod sursa (job #2402138) | Cod sursa (job #871485) | Cod sursa (job #2792296) | Cod sursa (job #2509197)
#include <bits/stdc++.h>
#define LSB(x) ((-x) & x)
#define NM 100100
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
int n,a[NM], Res[NM], lst[NM], AIB[NM], dp[NM], up[NM], bst;
void update(int x, int val)
{
int i;
for(int i = x;i <= n;i += LSB(i))
if(dp[val] > dp[AIB[i]])
AIB[i] = val;
}
int query(int x)
{
int mx = 0;
for(int i = x;i > 0;i -= LSB(i))
if(dp[AIB[i]] > dp[mx])
mx = AIB[i];
return mx;
}
int main()
{
in >> n;
int h = 1;
for(int i = 1;i <= n;i++)
{
in >> a[i];
Res[i] = lst[i] = a[i];
}
sort(lst + 1,lst + n + 1);
for(int i = 2;i <= n;i++)
if(lst[i] != lst[h])
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[bst] < dp[i])
bst = i;
out << dp[bst] << '\n';
h = 0;
for(int i = bst; i >= 1 ;i = up[i])
lst[++h] = Res[i];
for(int i = h;i >= 1;i--)
out << lst[i] << " ";
return 0;
}