Cod sursa(job #2509197)

Utilizator Bogdy_PPrunescu Bogdan Bogdy_P Data 13 decembrie 2019 22:24:54
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#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;
}