Cod sursa(job #2533080)

Utilizator TeodorCotetCotet Teodor TeodorCotet Data 28 ianuarie 2020 19:06:25
Problema Subsir crescator maximal Scor 35
Compilator py Status done
Runda Arhiva educationala Marime 0.99 kb
def read_gen(fname):
    with open(fname, 'rt') as fin:
        for line in fin:
            for val in line.split():
                yield int(val)
def bs(n, x, v):
    step = 1
    while step <= n: step <<= 1
    pos = 0
    while step > 0:
        if pos + step <= n and v[pos + step] < x:
            pos += step
        step >>= 1
    return pos + 1

def solve(n, a):
    """ d[i] = sequence of length i ending in smallest possible value d[i]"""
    d = [0 for _ in range(n + 1)]
    max_pos = 0
    for i in range(1, n + 1):
        pos = bs(max_pos, a[i], d)
        d[pos] = a[i]
        if pos > max_pos:   max_pos = pos
    return max_pos

if __name__ == "__main__":
    it = read_gen('scmax.in')
    n = next(it)
    a = [0 for _ in range(n + 1)]
    for i in range(1, n + 1):
        a[i] = next(it)
    ans = solve(n, a)   
    with open('scmax.out', 'wt') as fout:
        fout.write('{}\n'.format(ans))
        for _ in range(ans):
            fout.write('{} '.format(0))
        fout.write('\n')