Cod sursa(job #2009936)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 11 august 2017 12:26:06
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.1 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

class AIB
{
public:
    AIB(int sz)
    {
        v = new int[sz + 1];
        n = sz;
        for(int i = 1; i <= n; ++i)
            v[i] = 0;
    }
    AIB(int vec[], int sz)
    {
        v = new int[sz + 1];
        n = sz;
        for(int i = 1; i <= n; ++i)
            v[i] = 0;
        for(int i = 1; i <= n; ++i)
            Update(i, vec[i]);
    }
    ~AIB()
    {
        delete[] v;
    }
    void Update(int ind, int val)
    {
        while(ind <= n)
        {
            v[ind] = max(v[ind], val);
            ind += ind & (-ind);
        }
    }
    int Query(int dr)
    {
        int ret = 0;
        while(dr >= 1)
        {
            ret = max(ret, v[dr]);
            dr -= dr & (-dr);
        }
        return ret;
    }
private:
    int * v;
    int n;
};

const int nMax = 100005;

int n;
int v[nMax], cpy[nMax], norm[nMax], dp[nMax], last[nMax], pos[nMax];
int rasp, raspPos;

void citire()
{
    ifstream in("scmax.in");
    in >> n;
    for(int i = 1; i <= n; ++i)
        in >> v[i];
    in.close();
}

void rezolvare()
{
    for(int i = 1; i <= n; ++i)
        cpy[i] = norm[i] = v[i];
    sort(norm + 1, norm + n + 1);
    for(int i = 1; i <= n; ++i)
        v[i] = lower_bound(norm + 1, norm + n + 1, v[i]) - norm;
    AIB aib(n);
    int mx;
    for(int i = 1; i <= n; ++i)
    {
        mx = aib.Query(v[i] - 1);
        dp[i] = 1 + mx;
        pos[dp[i]] = i;
        last[i] = pos[mx];
        aib.Update(v[i], dp[i]);
        if(dp[i] > rasp)
        {
            rasp = dp[i];
            raspPos = i;
        }
    }
}

void afisare()
{
    ofstream out("scmax.out");
    out << rasp << "\n";
    vector<int> ans;
    int pos = raspPos;
    for(int i = 1; i <= rasp; ++i)
    {
        ans.push_back(cpy[pos]);
        pos = last[pos];
    }
    for(int i = ans.size() - 1; i >= 0; --i)
        out << ans[i] << " ";
    out.close();
}

int main()
{
    citire();
    rezolvare();
    afisare();
    return 0;
}