結果
問題 | No.1804 Intersection of LIS |
ユーザー |
![]() |
提出日時 | 2022-01-09 13:09:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 374 ms / 2,000 ms |
コード長 | 1,813 bytes |
コンパイル時間 | 248 ms |
コンパイル使用メモリ | 82,192 KB |
実行使用メモリ | 144,584 KB |
最終ジャッジ日時 | 2024-11-14 10:21:19 |
合計ジャッジ時間 | 8,382 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
import sys#input = sys.stdin.readline #文字列につけてはダメinput = sys.stdin.buffer.readline #文字列につけてはダメ#sys.setrecursionlimit(1000000)import bisect#import itertools#import random#from heapq import heapify, heappop, heappush#from collections import defaultdict#from collections import deque#import copy#from functools import lru_cachedef LIS(N, A): #狭義単調増加部分列INF = pow(10,18)#dp[i]: 最長増加部分列がi個の時の最後の値の最小値dp = [INF]*(N+1)maxlis = [0]*N #i番目を最後に使ってできる狭義単調増加部分列の最大値 - 1for i,x in enumerate(A):idx = bisect.bisect_left(dp, x) #x未満の数字を探して更新。dp[idx] = x #min(x, dp[idx])maxlis[i] = idx#print(dp)#辞書順最小の復元target = max(maxlis) #今探したいところ。ret = [0]*(target + 1)for i in reversed(range(N)):#一番最後に出てきているtargetのindexならそれを採用。if maxlis[i] == target:ret[target] = A[i]target -= 1#INF未満となるIndexを返す。辞書順最小のLISも返す。return bisect.bisect_left(dp, INF), retdef LDS(N, A):B = []for i in reversed(range(N)):a = A[i]B.append(-a)length, X = LIS(N, B) #-をつけた逆順で返ってくる。ret = []for i in reversed(range(len(X))):ret.append(-X[i])return length, retdef main():n = int(input())P = list(map(int,input().split()))m, jisyo0 = LIS(n,P)m, jisyo1 = LDS(n,P)ans = []for a,b in zip(jisyo0, jisyo1):if a == b: ans.append(a)print(len(ans))print(*ans)if __name__ == '__main__':main()