Cod sursa(job #2249635)

Utilizator AlexutAlex Calinescu Alexut Data 30 septembrie 2018 09:46:35
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;

FILE *fin, *fout;

int v[100001], pred[100001], pozmin[100001];

int cautBin( int nr, int n ) {
    int r = 1, pas = 1 << 16;
    while ( pas != 0 ) {
        if ( r + pas <= n && v[pozmin[r + pas]] < nr ) {
            r += pas;
        }
        pas /= 2;
    }
    return r;
}

void afisare( int i ) {
    if ( pred[i] != 0 )
        afisare( pred[i] );
    fprintf( fout, "%d ", v[i] );
}

int main() {
    int n, i, m, j;
    fin = fopen( "scmax.in", "r" );
    fout = fopen( "scmax.out", "w" );
    fscanf( fin, "%d", &n );
    m = 1;
    for ( i = 1; i <= n; i++ ) {
        fscanf( fin, "%d", &v[i] );
        j = cautBin( v[i], m );
        pred[i] = pozmin[j];
        pozmin[j + 1] = i;
        if ( j == m ) {
            m++;
        }
    }
    fprintf( fout, "%d\n", m - 1 );
    afisare( pozmin[m] );
    fclose( fin );
    fclose( fout );
    return 0;
}