Cod sursa(job #1211692)

Utilizator fromzerotoheroFROM ZERO fromzerotohero Data 23 iulie 2014 01:45:07
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#define nmax 100005
ifstream in("scmax.in");
ofstream out("scmax.out");

int n,i,j;
int A[nmax],S[nmax];
vector <int> Poz[nmax];

int caut_bin(int lo, int hi, int val) {
    
    while (lo<hi) {
        
        int mid = (hi + lo) / 2;
        
        if (S[mid] < val && S[mid + 1] >= val)
            return mid;
        
        if (S[mid] < val)
            lo = mid + 1;
        else
            hi = mid - 1;
        
    }
    
    return hi;
}

void citire() {
    
    in >> n;
    
    for (i=1; i<=n; i++)
        in >> A[i];
    
}

void tipareste(int hi) {
    
    int indice = Poz[hi][Poz[hi].size() - 1], k = 0;
    int Rev[nmax];
    
    Rev[++k] = A[indice];
    
    for (i=hi - 1; i>=0; i--)
        for (j=Poz[i].size() - 1; j>=0; j--)
            if (Poz[i][j] < indice) {
                indice = Poz[i][j];
                Rev[++k] = A[indice];
                break;
            }
    
    for (i=k; i>0; i--)
        out << Rev[i] << " ";
    
}

void rezolvare() {
    
    int hi = 0,poz;
    
    for (i=1; i<=n; i++) {
        
        poz = caut_bin(0, hi, A[i]);
        S[poz + 1] = A[i];
        hi = max(hi, poz + 1);
        
        Poz[poz + 1].push_back(i);
        
    }
    
    out << hi << "\n";
    
    tipareste(hi);
    
}

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