結果

問題 No.284 門松と魔法(2)
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-30 00:51:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,042 bytes
コンパイル時間 444 ms
コンパイル使用メモリ 10,984 KB
実行使用メモリ 59,172 KB
最終ジャッジ日時 2023-09-27 00:26:50
合計ジャッジ時間 15,275 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,800 KB
testcase_01 AC 19 ms
8,644 KB
testcase_02 AC 20 ms
8,580 KB
testcase_03 AC 20 ms
8,632 KB
testcase_04 AC 20 ms
8,568 KB
testcase_05 AC 20 ms
8,724 KB
testcase_06 WA -
testcase_07 AC 19 ms
8,768 KB
testcase_08 AC 19 ms
8,804 KB
testcase_09 AC 20 ms
8,716 KB
testcase_10 AC 21 ms
8,872 KB
testcase_11 AC 20 ms
8,668 KB
testcase_12 AC 20 ms
8,712 KB
testcase_13 AC 19 ms
8,784 KB
testcase_14 AC 20 ms
8,840 KB
testcase_15 AC 19 ms
8,796 KB
testcase_16 AC 20 ms
8,660 KB
testcase_17 AC 20 ms
8,784 KB
testcase_18 AC 21 ms
8,764 KB
testcase_19 AC 20 ms
8,716 KB
testcase_20 AC 20 ms
8,704 KB
testcase_21 AC 20 ms
8,712 KB
testcase_22 AC 21 ms
8,772 KB
testcase_23 AC 73 ms
11,496 KB
testcase_24 AC 3,386 ms
44,704 KB
testcase_25 AC 1,848 ms
22,924 KB
testcase_26 AC 20 ms
8,688 KB
testcase_27 AC 19 ms
8,684 KB
testcase_28 AC 19 ms
8,712 KB
testcase_29 AC 935 ms
59,172 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
    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

#import random
#N = 10000
#As = [random.randint(1, 100) for i in range(N)]

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