結果
問題 | No.1804 Intersection of LIS |
ユーザー |
|
提出日時 | 2024-09-15 02:25:03 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 394 ms / 2,000 ms |
コード長 | 3,381 bytes |
コンパイル時間 | 193 ms |
コンパイル使用メモリ | 82,528 KB |
実行使用メモリ | 146,984 KB |
最終ジャッジ日時 | 2024-09-15 02:25:13 |
合計ジャッジ時間 | 9,629 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
## https://yukicoder.me/problems/no/818class SegmentTree:"""非再帰版セグメント木。更新は「加法」、取得は「最大値」のもの限定。"""def __init__(self, init_array):n = 1while n < len(init_array):n *= 2self.size = nself.array = [0] * (2 * self.size)for i, a in enumerate(init_array):self.array[self.size + i] = aend_index = self.sizestart_index = end_index // 2while start_index >= 1:for i in range(start_index, end_index):self.array[i] = max(self.array[2 * i], self.array[2 * i + 1])end_index = start_indexstart_index = end_index // 2def add(self, x, a):index = self.size + xself.array[index] += awhile index > 1:index //= 2self.array[index] = max(self.array[2 * index], self.array[2 * index + 1])def get_max(self, l, r):L = self.size + l; R = self.size + r# 2. 区間[l, r)の最大値を求めるs = 0while L < R:if R & 1:R -= 1s = max(s, self.array[R])if L & 1:s = max(s, self.array[L])L += 1L >>= 1; R >>= 1return sdef main():N = int(input())P = list(map(int, input().split()))# LISを計算・それと同時に辞書順最小のLISの部分列を求められるようにするlength_array = [float("inf")] * (N + 1)length_array[0] = 0prev = [-2] * (N + 1)for p in P:low = 0high = Nwhile high - low > 1:mid = (high + low) // 2if length_array[mid] < p:low = midelse:high = midif length_array[high] < p:v = high + 1else:v = low + 1length_array[v] = pprev[p] = length_array[v - 1]lis = 0for i in range(N + 1):if length_array[i] < float("inf"):lis = imin_path = []s = length_array[lis]while s != 0:min_path.append(s)s = prev[s]min_path.reverse()# 今度は辞書順最大のLISを求める# LISを計算・それと同時に辞書順最小のLISの部分列を求められるようにするlength_array = [-float("inf")] * (N + 1)length_array[0] = 10 * Nprev = [-2] * (N + 1)for p in reversed(P):low = 0high = Nwhile high - low > 1:mid = (high + low) // 2if length_array[mid] > p:low = midelse:high = midif length_array[high] > p:v = high + 1else:v = low + 1length_array[v] = pprev[p] = length_array[v - 1]lis = 0for i in range(N + 1):if length_array[i] > -float("inf"):lis = imax_path = []s = length_array[lis]while s != 10 * N:max_path.append(s)s = prev[s]answer = []for i in range(len(max_path)):if min_path[i] == max_path[i]:answer.append(min_path[i])print(len(answer))print(" ".join(map(str, answer)))if __name__ == "__main__":main()