結果
| 問題 |
No.3303 Heal Slimes 2
|
| コンテスト | |
| ユーザー |
mkawa2
|
| 提出日時 | 2025-10-06 19:06:33 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,674 ms / 4,000 ms |
| コード長 | 2,371 bytes |
| コンパイル時間 | 434 ms |
| コンパイル使用メモリ | 82,360 KB |
| 実行使用メモリ | 114,448 KB |
| 最終ジャッジ日時 | 2025-10-21 10:24:59 |
| 合計ジャッジ時間 | 28,237 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 |
ソースコード
import sys
# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)
# md = 10**9+7
md = 998244353
from heapq import *
class SlopeTrick:
def __init__(self):
self.mn = 0
self.ll = [inf]
self.rr = [inf]
self.dl = [inf+1]
self.dr = [inf+1]
# 範囲x>=aにx-aを追加する
def add_inc(self, x):
while self.ll[0]==self.dl[0]:heappop(self.ll);heappop(self.dl)
self.mn += max(0, -self.ll[0]-x)
heappush(self.rr, -heappushpop(self.ll, -x))
# 範囲x<=aに-x+aを追加する
def add_dec(self, x):
while self.rr[0]==self.dr[0]:heappop(self.rr);heappop(self.dr)
self.mn += max(0, x-self.rr[0])
heappush(self.ll, -heappushpop(self.rr, x))
# 範囲x>=aからx-aを削除する
def del_inc(self, x):
while self.rr[0]==self.dr[0]:heappop(self.rr);heappop(self.dr)
if x>=self.rr[0]:
heappush(self.dr,x)
return
self.mn -= self.rr[0]-x
heappush(self.dl,-x)
heappush(self.ll, -heappop(self.rr))
# 範囲x<=aから-x+aを削除する
def del_dec(self, x):
while self.ll[0]==self.dl[0]:heappop(self.ll);heappop(self.dl)
if x<=-self.ll[0]:
heappush(self.dl,-x)
return
self.mn -= x+self.ll[0]
heappush(self.dr,x)
heappush(self.rr, -heappop(self.ll))
def get_min(self):
return self.mn
n,k,d=LI()
hh=LI()
st=SlopeTrick()
for h in hh[:k-1]:
st.add_inc(h)
st.add_dec(h-d)
ans=inf
for i in range(k-1,n):
h=hh[i]
st.add_inc(h)
st.add_dec(h-d)
ans=min(ans,st.get_min())
h=hh[i-k+1]
st.del_inc(h)
st.del_dec(h-d)
print(ans)
mkawa2