結果

問題 No.284 門松と魔法(2)
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-30 03:27:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 3,030 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 11,080 KB
実行使用メモリ 62,540 KB
最終ジャッジ日時 2023-09-27 00:27:34
合計ジャッジ時間 12,611 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,920 KB
testcase_01 AC 20 ms
8,932 KB
testcase_02 AC 20 ms
8,868 KB
testcase_03 AC 20 ms
8,848 KB
testcase_04 AC 20 ms
8,760 KB
testcase_05 AC 20 ms
8,944 KB
testcase_06 AC 19 ms
8,756 KB
testcase_07 AC 19 ms
8,896 KB
testcase_08 AC 20 ms
8,920 KB
testcase_09 AC 19 ms
8,712 KB
testcase_10 AC 21 ms
8,864 KB
testcase_11 AC 20 ms
8,956 KB
testcase_12 AC 20 ms
8,948 KB
testcase_13 AC 20 ms
8,800 KB
testcase_14 AC 20 ms
8,904 KB
testcase_15 AC 20 ms
8,776 KB
testcase_16 AC 20 ms
8,936 KB
testcase_17 AC 20 ms
8,736 KB
testcase_18 AC 20 ms
8,772 KB
testcase_19 AC 20 ms
8,860 KB
testcase_20 AC 20 ms
8,808 KB
testcase_21 AC 20 ms
8,848 KB
testcase_22 AC 20 ms
8,900 KB
testcase_23 AC 67 ms
12,020 KB
testcase_24 AC 1,990 ms
48,904 KB
testcase_25 AC 1,110 ms
24,444 KB
testcase_26 AC 19 ms
8,900 KB
testcase_27 AC 19 ms
8,848 KB
testcase_28 AC 20 ms
8,800 KB
testcase_29 AC 843 ms
62,540 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
#import time

#start = time.time()

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')])]
    maxLH = [float('inf')]
    LHprev = collections.defaultdict(int)
    LHrank = collections.defaultdict(int)
    LHprev[float('inf')] = -float('inf')
    LHrank[float('inf')] = 0
    
    HL = [set([-float('inf')])]
    minHL = [-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, maxLH, LHprev, HL, minHL, HLrank, HLprev)
        update_data(a, False, HL, minHL, HLprev, LH, maxLH, LHrank, LHprev)
    
    return max(len(LH), len(HL)) - 1


def update_data(a, dLOW, LH, maxLH, LHprev, HL, minHL, 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
        if (dLOW and maxLH[k] <= a) or (not dLOW and maxLH[k] >= a):
            continue
        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 (dLOW and a < minHL[k + 1]) or ((not dLOW) and a > minHL[k + 1]):
                    minHL[k + 1] = a
                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]))
                    dummy = float('inf') if dLOW else -float('inf')
                    minHL.append(dummy)
                    minHL.append(a)
                elif k + 1 == lenHL:
                    HL.append(set([a]))
                    minHL.append(a)
                elif k + 1 < lenHL:
                    HL[k + 1].add(a)
                    if  (dLOW and a < minHL[k + 1]) or ((not dLOW) and a > minHL[k + 1]):
                        minHL[k + 1] = 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