結果

問題 No.1435 Mmm......
ユーザー shotoyooshotoyoo
提出日時 2021-03-19 22:41:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,664 ms / 2,000 ms
コード長 2,048 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 113,608 KB
最終ジャッジ日時 2024-04-29 18:00:20
合計ジャッジ時間 28,426 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
52,480 KB
testcase_01 AC 47 ms
52,608 KB
testcase_02 AC 43 ms
52,736 KB
testcase_03 AC 44 ms
52,608 KB
testcase_04 AC 44 ms
52,608 KB
testcase_05 AC 44 ms
52,864 KB
testcase_06 AC 944 ms
96,128 KB
testcase_07 AC 1,247 ms
98,812 KB
testcase_08 AC 1,523 ms
104,192 KB
testcase_09 AC 1,252 ms
100,992 KB
testcase_10 AC 1,187 ms
98,656 KB
testcase_11 AC 1,099 ms
97,792 KB
testcase_12 AC 1,351 ms
98,048 KB
testcase_13 AC 981 ms
96,384 KB
testcase_14 AC 882 ms
90,240 KB
testcase_15 AC 1,664 ms
104,320 KB
testcase_16 AC 1,228 ms
98,100 KB
testcase_17 AC 940 ms
92,672 KB
testcase_18 AC 1,483 ms
104,704 KB
testcase_19 AC 997 ms
96,384 KB
testcase_20 AC 1,313 ms
100,992 KB
testcase_21 AC 402 ms
113,608 KB
testcase_22 AC 556 ms
106,208 KB
testcase_23 AC 1,265 ms
101,248 KB
testcase_24 AC 1,635 ms
101,376 KB
testcase_25 AC 1,424 ms
101,408 KB
testcase_26 AC 1,624 ms
101,276 KB
testcase_27 AC 1,617 ms
101,668 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
class HST:
    def __init__(self, ascending=True):
        self._h = []
        self._q = []
        self.ascending = ascending
    def push(self, v):
        if not self.ascending:
            v *= -1
        hp(self._h, v)
    def remove(self, v):
        if not self.ascending:
            v *= -1
        hp(self._q, 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._h[0]
            if not self.ascending:
                res *= -1
        return res
    def second(self):
        v = self.top()
        if v is None:
            return None
        self.remove(v)
        v2 = self.top()
        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()
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