結果
問題 | No.1435 Mmm...... |
ユーザー | shotoyoo |
提出日時 | 2021-03-19 22:41:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,654 ms / 2,000 ms |
コード長 | 2,048 bytes |
コンパイル時間 | 180 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 113,480 KB |
最終ジャッジ日時 | 2024-11-18 23:48:28 |
合計ジャッジ時間 | 28,355 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 24 |
ソースコード
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)