結果

問題 No.1435 Mmm......
ユーザー rlangevin
提出日時 2023-08-18 12:23:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,498 ms / 2,000 ms
コード長 1,119 bytes
コンパイル時間 212 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 112,916 KB
最終ジャッジ日時 2024-11-27 17:24:18
合計ジャッジ時間 21,734 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *
from collections import *
import sys
input = sys.stdin.readline
 
class QuasiBinaryTree:
    def __init__(self, rule="min"):
        if rule == "max":
            self.pm = -1
        else:
            self.pm = 1
 
        self.inf = 10**18
        self.P = [self.inf]
        self.Q = []
 
    def insert(self, x):
        heappush(self.P, x * self.pm)
 
    def erase(self, x):
        heappush(self.Q, x * self.pm)
 
    def top(self):
        while self.Q and (self.P[0] == self.Q[0]):
            heappop(self.P)
            heappop(self.Q)
        return self.P[0] * self.pm
    



N = int(input())
A = list(map(int, input().split()))

T1, T2 = QuasiBinaryTree("min"), QuasiBinaryTree("max")
Q = deque()
ans = 0
for i in range(N):
    T1.insert(A[i])
    T2.insert(A[i])
    Q.append(A[i])
    while len(Q) >= 2:
        ma = T2.top()
        mi = T1.top()
        T1.erase(mi)
        mi2 = T1.top()
        T1.insert(mi)
        if mi + mi2 >= ma:
            break
        else:
            q = Q.popleft()
            T1.erase(q)
            T2.erase(q)
    ans += len(Q) - 1

print(ans)
0