結果
問題 | No.1526 Sum of Mex 2 |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:59:32 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,513 bytes |
コンパイル時間 | 193 ms |
コンパイル使用メモリ | 82,112 KB |
実行使用メモリ | 110,228 KB |
最終ジャッジ日時 | 2025-03-31 18:00:40 |
合計ジャッジ時間 | 8,872 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 TLE * 1 -- * 1 |
ソースコード
def main():import sysinput = sys.stdin.read().split()n = int(input[0])a = list(map(int, input[1:n+1]))# Precompute the positions for each number (1-based)from collections import defaultdictpos = defaultdict(list)for idx, num in enumerate(a):pos[num].append(idx)res = 0for x in range(0, n + 2):# Check if mex is x+1target = x + 1# Determine the segments where target does not appearsplit_points = [-1]if target in pos:split_points.extend(pos[target])split_points.append(n)# Iterate through each segmentfor i in range(len(split_points) - 1):left = split_points[i] + 1right = split_points[i + 1] - 1if left > right:continue# Check if x can be considered for mex x+1if x == 0:# mex=1 requires that segment contains no 1. But target is 1, and split_points is [positions of 1], so segments are without 1# mex=1 is the count of subarrays in this segmentlength = right - left + 1res += (length * (length + 1) // 2) * (x + 1)continue# For x >= 1, we need 1..x to be present in the segment# Check if all 1..x exist in the arrayexists = Truefor num in range(1, x + 1):if not pos[num]:exists = Falsebreakif not exists:continue# Check if in this segment, all 1..x are present# Precompute the positions within the segmentlast = [-1] * (x + 2) # last occurrence of each numberrequired = set(range(1, x + 1))current = set()left_min = -1cnt = 0l = leftfor r in range(left, right + 1):num_r = a[r]if 1 <= num_r <= x:if last[num_r] < l:current.add(num_r)last[num_r] = r# Check if all required numbers are presentif current == required:# find the minimal last positionmin_last = min(last[1:x + 1])cnt += (min_last - l + 1)# else: can't contributeres += cnt * (x + 1)print(res)if __name__ == "__main__":main()