Cod sursa(job #2720561)

Utilizator Tudor_PascaTudor Pasca Tudor_Pasca Data 10 martie 2021 23:25:54
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stack>

using namespace std;

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

int n;
int index[100100], sub[100100];
stack<int> s;
pair<int, int> ans = {-1, -1};
pair<int, int> v[100100], bit[100100];

bool comp(pair<int, int> a, pair<int, int> b)
{
    if(a.first == b.first)
        return (a.second > b.second);

    return (a.first < b.first);
}

void update(int poz, pair<int, int> val)
{
    for(int i = poz; i <= n; i += i & (-i))
        if(bit[i].first < val.first)
            bit[i] = val;
}

pair<int, int> query(int poz)
{
    pair<int, int> ans = {0, -1};

    for(int i = poz; i > 0; i -= i & (-i))
        if(ans.first < bit[i].first)
            ans = bit[i];

    return ans;
}

int main()
{
    in >> n;

    for(int i = 1; i <= n; i++)
    {
        in >> v[i].first;
        v[i].second = i;
        index[v[i].second] = v[i].first;
    }

    sort(v + 1, v + n + 1, comp);

    for(int i = 1; i <= n; i++)
    {
        pair<int, int> val = query(v[i].second);

        update(v[i].second, {val.first + 1, v[i].second});

        sub[v[i].second] = val.second;

        if(ans.first < val.first + 1)
        {
            ans.first = val.first + 1;
            ans.second = v[i].second;
        }
    }

    out << ans.first << '\n';

    int x = ans.second;

    while(sub[x] != -1)
    {
        s.push(index[x]);
        x = sub[x];
    }

    s.push(index[x]);

    while(!s.empty())
    {
        out << s.top() << ' ';
        s.pop();
    }

    out << '\n';

    return 0;
}