Cod sursa(job #3041678)

Utilizator ciacliboiiiciacli stefan ciacliboiii Data 1 aprilie 2023 00:49:53
Problema Subsir crescator maximal Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, aib[100005], poz[100005], p;
struct Elem
{
    int val, index;
}v[100005];
//#define fin cin
//#define fout cout
void update(int poz, int val)
{
    for(int i = poz; i <= n; i += i & (-i))
        aib[i] = max(aib[i], val);
}
int query(int poz)
{
    int s = 0;
    for(int i = poz; i; i -= i & (-i))
        s = max(s, aib[i]);
    return s;
}
int cmp(const Elem &l, const Elem &r)
{
    if(l.val == r.val)
        return l.index > r.index;
    return l.val < r.val;
}
void ext(Elem &x)
{
    fout << x.val << ' ';
}
void findd(int x)
{
    if(poz[x] > -1)
        findd(poz[x]);
    fout << v[x].val << ' ';
}
int main()
{
    fin >> n;
    for(int i = 0; i < n; ++ i)
    {
        fin >> v[i].val;
        //ans[i] = v[i].val;
        v[i].index = i + 1;
    }

    int maxul = 0, fin;
    sort(v, v + n, cmp);
    //for_each(v, v + n, ext);
    //fout << '\n';
    p = -1;
    for(int i = 0; i < n; ++ i)
    {
        int d = query(v[i].index - 1) + 1;
        if(maxul < d)
        {
            maxul = d;
            poz[i] = p;
            p = i;
        }
        update(v[i].index, d);
       /* for(int j = 1; j <= n; ++ j)
            fout << aib[j] << ' ';
        fout << '\n';*/
    }
    fout << maxul << '\n';
    findd(p);
    return 0;
}