結果
問題 |
No.1687 What the Heck?
|
ユーザー |
![]() |
提出日時 | 2021-09-24 21:33:24 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,216 bytes |
コンパイル時間 | 189 ms |
コンパイル使用メモリ | 82,180 KB |
実行使用メモリ | 105,516 KB |
最終ジャッジ日時 | 2024-07-05 10:08:31 |
合計ジャッジ時間 | 5,926 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 9 WA * 9 |
ソースコード
import sys input = sys.stdin.readline N = int(input()) a = list(map(int, input().split())) import heapq hpush = heapq.heappush hpop = heapq.heappop h = [] for i in range(N): if a[i] < N: hpush(h, (-(i + 1) * 2, a[i])) else: hpush(h, (-(i + 1), a[i])) class BIT: def __init__(self, n): self.n = n self.data = [0] * (n + 1) self.el = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s def add(self, i, x): self.el[i] += x while i <= self.n: self.data[i] += x i += i & -i def get(self, i, j = None): if j is None: return self.el[i] return self.sum(j) - self.sum(i) def lowerbound(self, s): x = 0 y = 0 for i in range(self.n.bit_length(), -1, -1): k = x + (1 << i) if k <= self.n and (y + self.data[k] < s): y += self.data[k] x += 1 << i return x + 1 fwk = BIT(N + 1) for x in range(1, N + 1): fwk.add(x, 1) res = -N * (N + 1) // 2 while len(h): c, x = hpop(h) c = -c t = fwk.sum(x) y = fwk.lowerbound(t + 1) if x == N and fwk.get(N): fwk.add(N, -1) res += c continue if y > N: continue fwk.add(y, -1) res += c print(res)