結果

問題 No.1435 Mmm......
ユーザー shotoyooshotoyoo
提出日時 2021-03-19 22:41:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,606 ms / 2,000 ms
コード長 2,048 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 87,084 KB
実行使用メモリ 117,068 KB
最終ジャッジ日時 2023-08-12 03:27:26
合計ジャッジ時間 28,090 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,536 KB
testcase_01 AC 70 ms
71,340 KB
testcase_02 AC 70 ms
71,620 KB
testcase_03 AC 70 ms
71,460 KB
testcase_04 AC 72 ms
71,612 KB
testcase_05 AC 69 ms
71,300 KB
testcase_06 AC 923 ms
97,716 KB
testcase_07 AC 1,219 ms
100,020 KB
testcase_08 AC 1,489 ms
105,096 KB
testcase_09 AC 1,213 ms
101,904 KB
testcase_10 AC 1,166 ms
99,648 KB
testcase_11 AC 1,093 ms
99,684 KB
testcase_12 AC 1,341 ms
98,784 KB
testcase_13 AC 971 ms
97,592 KB
testcase_14 AC 849 ms
91,344 KB
testcase_15 AC 1,606 ms
105,056 KB
testcase_16 AC 1,156 ms
99,668 KB
testcase_17 AC 901 ms
93,500 KB
testcase_18 AC 1,404 ms
105,160 KB
testcase_19 AC 970 ms
97,840 KB
testcase_20 AC 1,270 ms
102,032 KB
testcase_21 AC 401 ms
117,068 KB
testcase_22 AC 556 ms
110,008 KB
testcase_23 AC 1,236 ms
107,376 KB
testcase_24 AC 1,581 ms
109,280 KB
testcase_25 AC 1,392 ms
107,472 KB
testcase_26 AC 1,547 ms
114,944 KB
testcase_27 AC 1,529 ms
109,748 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