結果

問題 No.1435 Mmm......
ユーザー shotoyooshotoyoo
提出日時 2021-03-20 00:12:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,722 ms / 2,000 ms
コード長 2,204 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 112,460 KB
最終ジャッジ日時 2024-04-29 20:13:37
合計ジャッジ時間 30,207 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
52,224 KB
testcase_01 AC 43 ms
52,352 KB
testcase_02 AC 43 ms
52,480 KB
testcase_03 AC 45 ms
53,120 KB
testcase_04 AC 44 ms
52,480 KB
testcase_05 AC 43 ms
52,864 KB
testcase_06 AC 974 ms
96,128 KB
testcase_07 AC 1,267 ms
98,256 KB
testcase_08 AC 1,584 ms
104,320 KB
testcase_09 AC 1,272 ms
101,248 KB
testcase_10 AC 1,196 ms
97,760 KB
testcase_11 AC 1,101 ms
97,804 KB
testcase_12 AC 1,374 ms
97,280 KB
testcase_13 AC 1,013 ms
96,896 KB
testcase_14 AC 885 ms
89,856 KB
testcase_15 AC 1,722 ms
104,704 KB
testcase_16 AC 1,220 ms
98,744 KB
testcase_17 AC 944 ms
92,032 KB
testcase_18 AC 1,487 ms
104,576 KB
testcase_19 AC 996 ms
97,152 KB
testcase_20 AC 1,346 ms
100,864 KB
testcase_21 AC 416 ms
112,460 KB
testcase_22 AC 577 ms
107,020 KB
testcase_23 AC 1,273 ms
101,504 KB
testcase_24 AC 1,682 ms
101,912 KB
testcase_25 AC 1,447 ms
101,376 KB
testcase_26 AC 1,639 ms
101,656 KB
testcase_27 AC 1,675 ms
102,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")

# 二分探索木の代わり BST HST
# 存在しない要素に対するremoveはこないことを仮定している
"""↓より少し遅い(?)が、同一要素を複数入れた場合に数を保持するd
"""
from heapq import heappop as hpp, heappush as hp
# 二分探索木の代わり BST HST
# 存在しない要素に対するremoveはこないことを仮定している
"""↓より少し遅い(?)が、同一要素を複数入れた場合に数を保持するd
"""
from heapq import heappop as hpp, heappush as hp
class HST:
    def __init__(self, s=1):
        self._h = []
        self._q = []
        self._s = s
    def push(self, v):
        hp(self._h, self._s*v)
    def remove(self, v):
        hp(self._q, self._s*v)
    def top(self):
        while self._q and self._q[0]==self._h[0]:
            hpp(self._q)
            hpp(self._h)
        if not self._h:
            res = None
        else:
            res = self._s*self._h[0]            
        return res
    def second(self):
        v = self.top()
        if v is None:
            return None
        self.remove(v)
        v2 = self.top()
        if v2 is not None:
            v2 *= self._s
        self.push(v)
        return v2

n = int(input())
a = list(map(int, input().split()))
from heapq import heappop as hpp, heappush as hp
h = HST()
H = HST(s=-1)
j = 0
ans = 0
ans0 = 0
for i in range(n):
    mi = 0
    while j<n:
        if j-i>=3:
            m0 = h.top()
            m1 = h.second()
            M = H.top()
            if m0+m1>=M:
                pass
            else:
                break
        h.push(a[j])
        H.push(a[j])
        j += 1
    m0 = h.top()
    m1 = h.second()
    M = H.top()
    if m1 is not None and m0+m1<M:
        ans += (j-i-2)
    else:
        ans += (j-i-1)
    h.remove(a[i])
    H.remove(a[i])
#     l = sorted(a[i:j])
#     if l[0]+l[1]<l[-1]:
#         ans0 += (j-i-2)
#     else:
#         ans0 += (j-i-1)
#     if ans!=ans0:
#         print(m0,m1,-M)
#         assert 0
print(ans)
0