結果

問題 No.1435 Mmm......
ユーザー titia
提出日時 2021-03-20 00:21:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,084 ms / 2,000 ms
コード長 1,244 bytes
コンパイル時間 394 ms
コンパイル使用メモリ 82,212 KB
実行使用メモリ 126,540 KB
最終ジャッジ日時 2024-11-19 03:27:43
合計ジャッジ時間 20,416 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
import heapq

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

MIN=[]

heapq.heappush(MIN,(A[0],0))
heapq.heappush(MIN,(A[1],1))

MAX=[]
heapq.heappush(MAX,(-A[0],0))
heapq.heappush(MAX,(-A[1],1))


ANS=0
now=1
for i in range(N):
    #print(i,now)
    
    while True:
        if now==N:
            ANS+=now-i-1
            break
        
        
        while MIN[0][1]<i:
            heapq.heappop(MIN)
        x,xi=heapq.heappop(MIN)

        while MIN[0][1]<i:
            heapq.heappop(MIN)
        y,yi=heapq.heappop(MIN)

        k=x+y

        while MAX[0][1]<i:
            heapq.heappop(MAX)

        l,li=heapq.heappop(MAX)

        if -l>k:
            ANS+=now-i-1
            heapq.heappush(MIN,(x,xi))
            heapq.heappush(MIN,(y,yi))
            heapq.heappush(MAX,(l,li))
            break
        else:
            now+=1
            heapq.heappush(MIN,(x,xi))
            heapq.heappush(MIN,(y,yi))
            heapq.heappush(MAX,(l,li))

            if now==N:
                continue

            heapq.heappush(MIN,(A[now],now))
            heapq.heappush(MAX,(-A[now],now))
            
    #print(i,now,k,l,MIN,MAX)

print(ANS)

            
        

    

    
    
    
0