結果

問題 No.284 門松と魔法(2)
ユーザー gew1fw
提出日時 2025-06-12 19:53:16
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,518 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,548 KB
実行使用メモリ 81,784 KB
最終ジャッジ日時 2025-06-12 19:54:16
合計ジャッジ時間 9,354 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23 TLE * 1 -- * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    n = int(sys.stdin.readline())
    A = list(map(int, sys.stdin.readline().split()))
    if n < 3:
        print(0)
        return

    max_length = 0
    single = defaultdict(int)
    pair = defaultdict(int)

    for c in A:
        new_pair = defaultdict(int)
        new_single = defaultdict(int)

        # Process existing pairs
        for (a, b), length in pair.items():
            if a != b and b != c and a != c:
                if (b > a and b > c) or (b < a and b < c):
                    key = (b, c)
                    if length + 1 > new_pair.get(key, 0):
                        new_pair[key] = length + 1

        # Process existing singles to form new pairs
        for a, length in single.items():
            key = (a, c)
            if 2 > new_pair.get(key, 0):
                new_pair[key] = 2

        # Update max_length from new_pair
        for key, length in new_pair.items():
            if length > max_length:
                max_length = length

        # Update pair and single
        for key, val in new_pair.items():
            if val > pair.get(key, 0):
                pair[key] = val
        for key, val in new_single.items():
            if val > single.get(key, 0):
                single[key] = val

        # Update single for current c
        single[c] = max(single.get(c, 0), 1)

    # Check if max_length is at least 3
    print(max_length if max_length >= 3 else 0)

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