Cod sursa(job #2687397)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 19 decembrie 2020 23:27:44
Problema Subsir crescator maximal Scor 70
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <stdio.h>
#include <ctype.h>
#define MAX_N 100000
#define BUFSIZE (128 * 1024)
FILE *fin, *fout;
int v[MAX_N], len[MAX_N], prev[MAX_N], rasp[MAX_N];
int rpos;
char rbuf[BUFSIZE];
static inline void initRead() {
    fin = fopen( "scmax.in", "r" );
    rpos = BUFSIZE - 1;
}
static inline char readChar() {
    if ( !(rpos = (rpos + 1) & (BUFSIZE - 1)) )
        fread( rbuf, 1, BUFSIZE, fin );
    return rbuf[rpos];
}
int readInt() {
    int ch, res = 0, semn = 1;
    while ( isspace( ch = readChar() ) );
    if ( ch == '-' ) {
        semn = -1;
        ch = readChar();
    }
    do
        res = 10 * res + ch - '0';
    while ( isdigit( ch = readChar() ) );
    return semn * res;
}
int main() {
    int n, maxLen, m, p, i, j;
    initRead();
    n = readInt();
    maxLen = 0;
    p = -1;
    for ( i = 0; i < n; i++ ) {
        v[i] = readInt();
        len[i] = 1;
        prev[i] = -1;
        for ( j = 0; j < i; j++ ) {
            if ( v[j] < v[i] ) {
                if ( len[j] + 1 > len[i] ) {
                    len[i] = len[j] + 1;
                    prev[i] = j;
                }
            }
        }
        if ( len[i] > maxLen ) {
            maxLen = len[i];
            p = i;
        }
    }
    fclose( fin );
    fout = fopen( "scmax.out", "w" );
    fprintf( fout, "%d\n", maxLen );
    m = 0;
    while ( p != -1 ) {
        rasp[m] = v[p];
        m++;
        p = prev[p];
    }
    for ( i = m - 1; i >= 0; i-- )
        fprintf( fout, "%d ", rasp[i] );
    fclose( fout );
    return 0;
}