結果
問題 | No.2006 Decrease All to Zero |
ユーザー | chineristAC |
提出日時 | 2022-02-23 03:03:06 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,178 ms / 4,000 ms |
コード長 | 2,477 bytes |
コンパイル時間 | 234 ms |
コンパイル使用メモリ | 82,392 KB |
実行使用メモリ | 179,596 KB |
最終ジャッジ日時 | 2024-12-20 16:40:41 |
合計ジャッジ時間 | 31,080 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
57,664 KB |
testcase_01 | AC | 48 ms
57,540 KB |
testcase_02 | AC | 334 ms
83,484 KB |
testcase_03 | AC | 54 ms
62,568 KB |
testcase_04 | AC | 50 ms
57,468 KB |
testcase_05 | AC | 50 ms
56,964 KB |
testcase_06 | AC | 50 ms
56,848 KB |
testcase_07 | AC | 147 ms
78,440 KB |
testcase_08 | AC | 68 ms
70,872 KB |
testcase_09 | AC | 65 ms
68,496 KB |
testcase_10 | AC | 3,031 ms
179,112 KB |
testcase_11 | AC | 3,178 ms
179,596 KB |
testcase_12 | AC | 3,012 ms
178,376 KB |
testcase_13 | AC | 2,990 ms
178,724 KB |
testcase_14 | AC | 3,119 ms
179,336 KB |
testcase_15 | AC | 3,175 ms
179,428 KB |
testcase_16 | AC | 1,352 ms
115,872 KB |
testcase_17 | AC | 2,404 ms
156,408 KB |
testcase_18 | AC | 46 ms
56,568 KB |
testcase_19 | AC | 48 ms
56,788 KB |
testcase_20 | AC | 47 ms
56,892 KB |
testcase_21 | AC | 49 ms
56,716 KB |
testcase_22 | AC | 52 ms
57,864 KB |
testcase_23 | AC | 48 ms
57,256 KB |
testcase_24 | AC | 48 ms
57,044 KB |
testcase_25 | AC | 49 ms
58,252 KB |
testcase_26 | AC | 2,858 ms
179,104 KB |
testcase_27 | AC | 1,381 ms
178,464 KB |
testcase_28 | AC | 1,391 ms
178,472 KB |
testcase_29 | AC | 325 ms
82,024 KB |
ソースコード
class DualSegmentTree: def __init__(self, n, segfunc, ide_ele): self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num def update(self,l,r,x): l += self.num r += self.num while l < r: if l & 1: self.tree[l] = self.segfunc(self.tree[l],x) l += 1 if r & 1: self.tree[r-1] = self.segfunc(self.tree[r-1],x) l >>= 1 r >>= 1 def __getitem__(self,idx): idx += self.num res = self.ide_ele while idx: res = self.segfunc(res,self.tree[idx]) idx>>=1 return res import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import log,gcd input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) INF = 10**18 N = int(input()) X = li() A = li() M = max(A) #assert M <= 200 if N==1: if A[0]==1: print(0) else: print(-1) exit() dp = [[INF]*(M+3) for p in range(3)] dp[0][A[0]] = (X[1]-X[0]) * (2 * A[0] - 2) dp[1][A[0]] = (X[1]-X[0]) * (2 * A[0] - 1) dp[2][A[0]] = (X[1]-X[0]) * (2 * A[0]) for i in range(1,N-1): #ndp = [[INF]*(M+3) for p in range(3)] ndp = [DualSegmentTree(M+3,min,INF) for p in range(3)] for j in range(1,M+3): #端におかない if j!=1: l = A[i]+j-2*min(A[i],j-1) r = min(M+2,A[i]+j) for p in range(3): ndp[p].update(l,r+1,dp[p][j]+(X[i+1]-X[i])*(p-2)) #片端におく l = A[i]+j-2*min(A[i],j)+1 r = min(M+2,A[i]+j) for p in range(2): ndp[p].update(l,r+1,dp[1][j]+(X[i+1]-X[i])*(p-2)) for p in range(1,3): ndp[p].update(l,r+1,dp[2][j]+(X[i+1]-X[i])*(p-2)) #p2,両端におく if 2 <= A[i]: l = A[i]+j-2*min(A[i],j+1)+2 r = min(M+2,A[i]+j) for p in range(3): ndp[p].update(l,r+1,dp[2][j]+(X[i+1]-X[i])*(p-2)) dp = [[ndp[p][j]+(X[i+1]-X[i])*2*j for j in range(M+3)] for p in range(3)] res = 10**17 for p in range(3): if p <= A[-1]: res = min(res,dp[p][A[-1]-p+1]) if res==10**17: print(-1) else: print(res)