Cod sursa(job #885537)

Utilizator abcdefg12345nume complet am zis abcdefg12345 Data 22 februarie 2013 08:55:04
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
using namespace std;
 
const int MAXN = 100005;
 
ifstream fin("scmax.in");
ofstream fout("scmax.out");
 
int N;
int A[MAXN];
int Best[MAXN];
int Father[MAXN];
 
int B[MAXN], LengthB;
 
void ReadData() {
    fin >> N;
    for (int i = 1; i <= N; ++i)
        fin >> A[i];
}
 
void Solve() {
    B[ LengthB = 0 ] = 0;
 
    int pow = 1;
    for (int i = 1; i <= N; ++i) {
        while ((pow << 1) <= LengthB) pow <<= 1;
 
        int ans = 0;
        for (int stp = pow; stp; stp >>= 1)
            if (ans + stp <= LengthB && A[B[ans + stp]] < A[i])
                ans += stp;
 
        Best[i] = Best[B[ans]] + 1;
        Father[i] = B[ans];
 
        if (ans == LengthB)
            B[++LengthB] = i;
        else
            if (A[B[ans+1]] > A[i])
                B[ans+1] = i;
    }
}
 
void ReconstSol(int poz) {
    if (poz == 0) return;
    ReconstSol(Father[poz]);
    fout << A[poz] << " ";
}
 
void WriteData() {
    fout << LengthB << endl;
    for (int i = 1; i <= N; ++i)
        if (Best[i] == LengthB) {
            ReconstSol(i);
            return;
        }
}
 
int main() {
    ReadData();
    Solve();
    WriteData();
}