Cod sursa(job #903467)

Utilizator SteveStefan Eniceicu Steve Data 1 martie 2013 21:06:13
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstring>
#include <deque>
#include <stack>
#include <bitset>
#include <list>
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp(a,b) make_pair (a, b)
#define ll long long
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)

using namespace std;

int N, L = 1;
int v[100011];
int smallest[100011];
int point[100011];
vector <int> Ans;

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

int B_Search (int val)
{
    int i, step = 1 << 17;
    for (i = 0; step; step >>= 1)
        if (i + step <= L && v[smallest[i + step]] < val) i += step;
    return i;
}

void Business ()
{
    smallest[1] = 1;
    for (int i = 2; i <= N; i++)
    {
        int a = B_Search (v[i]);
        point[i] = smallest[a];
        if (a + 1 > L)
        {
            L = a + 1;
            smallest[a + 1] = i;
        }
        else if (v[i] < v[smallest[a + 1]]) smallest[a + 1] = i;
    }
}

void Scriere ()
{
    ofstream fout ("scmax.out");
    fout << L << "\n";
    int alfa = smallest[L];
    for (; alfa != 0; alfa = point[alfa])
        Ans.pb (v[alfa]);
    for (int i = Ans.size () - 1; i >= 0; i--)
        fout << Ans[i] << " ";
    fout.close ();
}

int main ()
{
    Citire ();
    Business ();
    Scriere ();
    return 0;
}