結果
問題 |
No.1804 Intersection of LIS
|
ユーザー |
|
提出日時 | 2024-05-19 20:14:50 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 436 ms / 2,000 ms |
コード長 | 1,628 bytes |
コンパイル時間 | 178 ms |
コンパイル使用メモリ | 82,284 KB |
実行使用メモリ | 153,600 KB |
最終ジャッジ日時 | 2024-12-20 17:15:58 |
合計ジャッジ時間 | 9,648 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
# https://qiita.com/recuraki/items/9ba63b343660da2437e8 inf = 2 ** 63 - 1 from bisect import bisect_left def lis(l): # STEP1: LIS長パート with 使用位置 n = len(l) lisDP = [inf] * n # 通常のLIS用リスト indexList = [None] * n # lの[i]文字目が使われた場所を記録する for i in range(n): # 通常のLISを求め、indexListに使った場所を記録する ind = bisect_left(lisDP, l[i]) lisDP[ind] = l[i] indexList[i] = ind # STEP2: LIS復元パート by 元配列の使用した位置 # 後ろから見ていくので、まずは、LIS長目(targetIndex)のindexListを探したいとする targetIndex = max(indexList) ans = [0] * (targetIndex + 1) # 復元結果(indexListは0-indexedなのでlen=4ならmax=3で格納されているので+1する) # 後ろから見ていく for i in range(n - 1, -1, -1): # もし、一番最後に出てきているtargetIndexなら if indexList[i] == targetIndex: ans[targetIndex] = l[i] # ansのtargetIndexを確定 targetIndex -= 1 return ans #print(" ".join(list(map(str, lis([7,8,9,4,5,6,1]))))) # 4 5 6 #print(" ".join(list(map(str, lis([1,2,6,9,5,10]))))) # 1 2 6 9 10 #n = int(input()) #l = list(map(int, input().split())) #print(" ".join(list(map(str, lis(l))))) N = int(input()) P = list(map(int, input().split())) dp = lis(P) P = [-P[N - 1 - i] for i in range(N)] ep = lis(P) ep = [-ep[len(ep) - 1 - i] for i in range(len(ep))] ans = [] for x, y in zip(dp, ep): if x == y: ans.append(x) print(len(ans)) print(*ans)