Cod sursa(job #3041677)

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

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, aib[100005];
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 << ' ';
}
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';
    for(int i = 0; i < n; ++ i)
    {
        int d = query(v[i].index - 1) + 1;
        if(maxul < d)
        {
            maxul = d;
            fin = i;
        }
        update(v[i].index, d);
       /* for(int j = 1; j <= n; ++ j)
            fout << aib[j] << ' ';
        fout << '\n';*/
    }
    fout << maxul << '\n';
    for(int i = 0; i < fin; ++ i)
        if(v[i].index < v[fin].index && v[i].val > v[i - 1].val)
            fout << v[i].val << ' ';
    if(v[fin].val > v[fin - 1].val)
    fout << v[fin].val << ' ';
    return 0;
}