Cod sursa(job #2940338)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 15 noiembrie 2022 12:08:01
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.74 kb
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;

using namespace std;

const int NMAX = 1e5 + 5;
/*******************************/
// INPUT / OUTPUT

ifstream f("scmax.in");
ofstream g("scmax.out");
/*******************************/
/// GLOBAL DECLARATIONS

int N, v[NMAX], dp[NMAX], pre[NMAX];
int max_len, start_sol;
vector <int> sol;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> N;
    
    for (int i = 1 ; i <= N ; ++ i)
        f >> v[i];
}
///-------------------------------------
int cb(int x)
{
    int st = 1, dr = max_len, pos = 0;

    int mid;
    while (st <= dr)
    {
        mid = (st + dr) / 2;
        if (v[dp[mid]] < x)
        {
            pos = mid;
            st = mid + 1;
        }
        else
        {
            dr = mid - 1;
        }
    }

    return pos;
}
///-------------------------------------
inline void Solution()
{
    int j;
    for (int i = 1 ; i <= N ; ++ i)
    {
        j = cb(v[i]);
        if (max_len < j + 1)
        {
            max_len = j + 1;
            start_sol = i;
        }
        dp[j + 1] = i;
        pre[i] = dp[j];
    }

    while (start_sol)
    {
        sol.push_back(start_sol);
        start_sol = pre[start_sol];
    }
    reverse(sol.begin(), sol.end());
}
///-------------------------------------
inline void Output()
{
    g << max_len << "\n";

    for (auto x: sol)
        g << v[x] << " ";
}
///-------------------------------------
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ReadInput();
    Solution();
    Output();
    return 0;
}