結果

問題 No.1526 Sum of Mex 2
ユーザー lam6er
提出日時 2025-03-20 20:35:45
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,282 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 82,664 KB
実行使用メモリ 91,084 KB
最終ジャッジ日時 2025-03-20 20:37:18
合計ジャッジ時間 7,576 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 TLE * 1 -- * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    n = int(input[0])
    A = list(map(int, input[1:n+1]))
    
    present = {}
    for num in A:
        present[num] = True
    
    max_consecutive = 0
    current = 1
    while current in present:
        max_consecutive += 1
        current += 1
    
    def count_ge(k):
        if k == 1:
            return n * (n + 1) // 2
        m = k - 1
        if m > max_consecutive:
            return 0
        required = set(range(1, m + 1))
        freq = {}
        left = 0
        cnt = 0
        res = 0
        for right in range(n):
            num = A[right]
            if num in required:
                if num not in freq or freq[num] == 0:
                    cnt += 1
                freq[num] = freq.get(num, 0) + 1
            while cnt == len(required):
                res += n - right
                num_left = A[left]
                if num_left in required:
                    freq[num_left] -= 1
                    if freq[num_left] == 0:
                        cnt -= 1
                left += 1
        return res
    
    total = 0
    max_k = max_consecutive + 1
    for k in range(1, max_k + 1):
        total += count_ge(k)
    
    print(total)

if __name__ == '__main__':
    main()
0