結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー ygd.ygd.
提出日時 2021-01-29 21:56:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 453 ms / 2,000 ms
コード長 2,101 bytes
コンパイル時間 564 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 113,020 KB
最終ジャッジ日時 2023-09-09 15:19:40
合計ジャッジ時間 6,897 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,200 KB
testcase_01 AC 70 ms
71,160 KB
testcase_02 AC 450 ms
83,316 KB
testcase_03 AC 238 ms
80,108 KB
testcase_04 AC 319 ms
80,720 KB
testcase_05 AC 408 ms
112,972 KB
testcase_06 AC 442 ms
112,888 KB
testcase_07 AC 142 ms
113,020 KB
testcase_08 AC 453 ms
112,952 KB
testcase_09 AC 407 ms
111,272 KB
testcase_10 AC 412 ms
111,416 KB
testcase_11 AC 402 ms
111,212 KB
testcase_12 AC 364 ms
111,292 KB
testcase_13 AC 409 ms
111,212 KB
testcase_14 AC 410 ms
111,312 KB
testcase_15 AC 402 ms
111,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v):
        self.update(i, self[i] + v)

    def sum(self, l, r): #これで[l,r)から取り出す。
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)

def check(X):
    if X[0] == X[1] or X[1] == X[2] or X[2] == X[0]:
        return False
    if X[0] < X[1] and X[1] > X[2]:
        return True
    if X[0] > X[1] and X[1] < X[2]:
        return True
    return False

T = int(input()); INF = 1 << 60
for _ in range(T):
    N = int(input())
    A = list(map(int,input().split()))
    ans = 0
    for i in range(3):
        if i >= 1:
            temp = A[0]
            A = A[1:]
            A.append(temp)
        zero = [0]*(N+1)
        dp = SegmentTree(zero,max,0)
        for j in range(N-2): #0-N-2
            #print(A[j:j+3])
            if check(A[j:j+3]):
                #print("A",A[j:j+3],A[j])
                dp.update(j+1, A[j] + dp.sum(0,j-1)) #j+1(0-indexでj)番目を更新
                #print(dp.__getitem__(j+1),"koushin")
        ##print
        #for k in range(N+1):
        #    print(dp.__getitem__(k))
        ans = max(ans,dp.sum(0,N+1))
    print(ans)
0