cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub kobejean/cp-library

:heavy_check_mark: test/library-checker/other/longest_increasing_sequence.test.py

Depends on

Code

# verification-helper: PROBLEM https://judge.yosupo.jp/problem/longest_increasing_subsequence
def main():
    N = rd()
    A = rdl(N)
    ans = lis(A)
    wtn(len(ans))
    wtnl(ans)

from cp_library.alg.dp.lis_fn import lis
from cp_library.io.fast.fast_io_fn import rd, rdl, wtn, wtnl

main()
# verification-helper: PROBLEM https://judge.yosupo.jp/problem/longest_increasing_subsequence
def main():
    N = rd()
    A = rdl(N)
    ans = lis(A)
    wtn(len(ans))
    wtnl(ans)

'''
╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
             https://kobejean.github.io/cp-library               
'''
from bisect import bisect_left



def max2(a, b):
    return a if a > b else b

def chmin(dp, i, v):
    if ch:=dp[i]>v:dp[i]=v
    return ch

def lis(A: list):
    N = len(A)
    mn, mx = min(A), max(A)
    dp, idx, prev = [mx+1]*(N+1), [-1]*(N+1), [-1]*N
    dp[0], r = mn-1, 0
    for i,a in enumerate(A):
        if chmin(dp, p := bisect_left(dp,a,hi=r+1), a):
            idx[p], prev[i], r = i, idx[p-1], max2(r,p)
    ans, i = [0]*r, idx[r]
    for j in range(r-1,-1,-1): ans[j], i = i, prev[i]
    return ans

    

from __pypy__.builders import StringBuilder
import sys
from os import read as os_read, write as os_write
from atexit import register as atexist_register

class Fastio:
    ibuf = bytes()
    pil = pir = 0
    sb = StringBuilder()
    def load(self):
        self.ibuf = self.ibuf[self.pil:]
        self.ibuf += os_read(0, 131072)
        self.pil = 0; self.pir = len(self.ibuf)
    def flush_atexit(self): os_write(1, self.sb.build().encode())
    def flush(self):
        os_write(1, self.sb.build().encode())
        self.sb = StringBuilder()
    def fastin(self):
        if self.pir - self.pil < 64: self.load()
        minus = x = 0
        while self.ibuf[self.pil] < 45: self.pil += 1
        if self.ibuf[self.pil] == 45: minus = 1; self.pil += 1
        while self.ibuf[self.pil] >= 48:
            x = x * 10 + (self.ibuf[self.pil] & 15)
            self.pil += 1
        if minus: return -x
        return x
    def fastin_string(self):
        if self.pir - self.pil < 64: self.load()
        while self.ibuf[self.pil] <= 32: self.pil += 1
        res = bytearray()
        while self.ibuf[self.pil] > 32:
            if self.pir - self.pil < 64: self.load()
            res.append(self.ibuf[self.pil])
            self.pil += 1
        return res
    def fastout(self, x): self.sb.append(str(x))
    def fastoutln(self, x): self.sb.append(str(x)); self.sb.append('\n')
fastio = Fastio()
rd = fastio.fastin; rds = fastio.fastin_string; wt = fastio.fastout; wtn = fastio.fastoutln; flush = fastio.flush
atexist_register(fastio.flush_atexit)
sys.stdin = None; sys.stdout = None
def rdl(n): return [rd() for _ in range(n)]
def wtnl(l): wtn(' '.join(map(str, l)))

main()
Back to top page