fork download
  1. #!/usr/bin/env python3
  2. from math import ceil
  3.  
  4. def max_increasing_subseq_length(X):
  5. L = 0
  6. M = [None] * (len(X) + 1)
  7. for i, x in enumerate(X):
  8. # binary search for the largest positive j <= L such that X[M[j]] < X[i]
  9. lo, hi = 1, L
  10. while lo <= hi:
  11. mid = ceil((lo + hi) / 2)
  12. if X[M[mid]] < x:
  13. lo = mid + 1
  14. else:
  15. hi = mid - 1
  16.  
  17. # After searching, lo is 1 greater than the length of the longest
  18. # prefix of X[i]
  19. M[lo] = i
  20. if lo > L:
  21. L = lo
  22. return L
  23.  
  24. N, numbers = int(input()), list(map(int, input().split()))
  25. assert len(numbers) == N
  26. print(max_increasing_subseq_length(numbers))
Success #stdin #stdout 0.01s 9992KB
stdin
10
4 3 6 7 9 5 2 8 7 3
stdout
4