結果

問題 No.284 門松と魔法(2)
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-30 02:30:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,375 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 11,076 KB
実行使用メモリ 59,264 KB
最終ジャッジ日時 2023-09-27 00:27:17
合計ジャッジ時間 15,441 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,716 KB
testcase_01 AC 19 ms
8,844 KB
testcase_02 AC 20 ms
8,776 KB
testcase_03 AC 20 ms
8,792 KB
testcase_04 AC 19 ms
8,780 KB
testcase_05 AC 19 ms
8,792 KB
testcase_06 AC 20 ms
8,772 KB
testcase_07 AC 20 ms
8,856 KB
testcase_08 AC 19 ms
8,708 KB
testcase_09 AC 19 ms
8,712 KB
testcase_10 AC 21 ms
8,884 KB
testcase_11 AC 20 ms
8,724 KB
testcase_12 AC 20 ms
8,908 KB
testcase_13 AC 19 ms
8,696 KB
testcase_14 AC 20 ms
8,884 KB
testcase_15 AC 20 ms
8,752 KB
testcase_16 AC 20 ms
8,772 KB
testcase_17 AC 19 ms
8,724 KB
testcase_18 AC 20 ms
8,852 KB
testcase_19 AC 20 ms
8,720 KB
testcase_20 AC 19 ms
8,704 KB
testcase_21 AC 20 ms
8,728 KB
testcase_22 AC 21 ms
8,764 KB
testcase_23 AC 72 ms
11,516 KB
testcase_24 AC 3,387 ms
45,464 KB
testcase_25 AC 1,815 ms
23,060 KB
testcase_26 AC 19 ms
8,764 KB
testcase_27 AC 20 ms
8,636 KB
testcase_28 AC 19 ms
8,664 KB
testcase_29 AC 936 ms
59,264 KB
testcase_30 TLE -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

def solve(N, As):
    As = compress(As)
    if len(As) < 3:
        return 0
    if len(set(As)) < 7:
        return solve_small(As)
    LH = [set([float('inf')])]
    LHprev = collections.defaultdict(int)
    LHrank = collections.defaultdict(int)
    LHprev[float('inf')] = -float('inf')
    LHrank[float('inf')] = 0
    
    HL = [set([-float('inf')])]
    HLprev = collections.defaultdict(int)
    HLrank = collections.defaultdict(int)
    HLprev[-float('inf')] = float('inf')
    HLrank[-float('inf')] = 0
    
    for a in As:
        update_data(a, True, LH, LHprev, HL, HLrank, HLprev)
        update_data(a, False, HL, HLprev, LH, LHrank, LHprev)
    
    return max(len(LH), len(HL)) - 1


def update_data(a, dLOW, LH, LHprev, HL, HLrank, HLprev):
    '''dLOW: True  -> a をHL に追加しようとしている。
             False -> a をLH に追加しようとそている。
       LH, HL 等は、dLOW = True の場合の変数名。呼び出し側で適宜入れ替える。
    '''
    LHmax = len(LH) - 1
    for k in range(LHmax, - 1, -1):
        if k + 1 < HLrank[a]:
            break
        for b in LH[k]:
            if (dLOW and b <= a) or ((not dLOW) and b >= a) or LHprev[b] == a:
                continue
            if HLrank[a] == k + 1:
                if HLprev[a] != b:
                    HLprev[a] = -1
                break
            else:
                HLrank[a] = k + 1
                HLprev[a] = b
                lenHL = len(HL)
                if k == lenHL:
                    HL.append(set())
                    HL.append(set([a]))
                elif k + 1 == lenHL:
                    HL.append(set([a]))
                elif k + 1 < lenHL:
                    HL[k + 1].add(a)

def compress(As):
    Bs = [As[0]]
    b = As[0]
    for a in As[1:]:
        if a != b:
            Bs.append(a)
            b = a
    return Bs

    
def solve_small(As):
    dp = collections.defaultdict(int)
    dp[float('inf'), -float('inf')] = 0
    dp[-float('inf'), float('inf')] = 0
    for a in As:
        for (b, c), val in list(dp.items()):
            if c != a and ((a > b and b < c) or (a < b and b > c)):
                dp[a, b] = max(val + 1, dp[a, b])
    return max(dp.values())

N = int(input())
As = list(map(int, input().split()))
ans = solve(N, As)
if ans < 3:
    print(0)
else:
    print(ans)
0