Cod sursa(job #2469813)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 8 octombrie 2019 00:40:10
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 kb
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007

using namespace std;

typedef long long ll;

ifstream f("scmax.in");
ofstream g("scmax.out");

int n, v[100002], sorted[100002];

int aib[100002], fw[100002];

int dp[100002], du[100002];

void add(int poz, int val, int cn)
{
    for(; poz <= n; poz += (poz & (-poz)))
        if(val > aib[poz])
            aib[poz] = val, fw[poz] = cn;
}
pair<int, int> compute(int poz)
{
    pair<int, int> ans = {0, 0};
    for(; poz; poz -= (poz & (-poz)))
        if(aib[poz] > ans.fi)
            ans.fi = aib[poz], ans.se = fw[poz];
    return ans;
}
int cb(int val)
{
    int st = 1;
    int dr = n;
    int ans = 0;
    while(st <= dr)
    {
        int mid = (st + dr) / 2;
        if(sorted[mid] <= val)
            ans = mid, st = mid + 1;
        else
            dr = mid - 1;
    }
    return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> v[i], sorted[i] = v[i];
    sort(sorted + 1, sorted + n + 1);
    int ans = 0;
    for(int i = 1; i <= n; ++i)
    {
        int pz = cb(v[i]);
        pair<int, int> bst = compute(pz - 1);
        bst.fi++;
        ans = max(ans, bst.fi);
        du[i] = bst.se;
        dp[i] = bst.fi;
        add(pz, bst.fi, i);
    }
    g << ans << '\n';
    for(int i = n; i >= 1; --i)
        if(dp[i] == ans)
        {
            vector<int>vv;
            while(i)
            {
                vv.pb(v[i]);
                i = du[i];
            }
            sort(vv.begin(), vv.end());
            for(int j = 0; j < ans; ++j)
                g << vv[j] << " ";
            return 0;
        }
    return 0;
}