結果
問題 | No.2006 Decrease All to Zero |
ユーザー | chineristAC |
提出日時 | 2022-02-23 03:03:06 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,209 ms / 4,000 ms |
コード長 | 2,477 bytes |
コンパイル時間 | 339 ms |
コンパイル使用メモリ | 82,364 KB |
実行使用メモリ | 179,844 KB |
最終ジャッジ日時 | 2024-05-19 07:33:14 |
合計ジャッジ時間 | 31,648 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
57,480 KB |
testcase_01 | AC | 47 ms
56,804 KB |
testcase_02 | AC | 323 ms
83,864 KB |
testcase_03 | AC | 51 ms
63,904 KB |
testcase_04 | AC | 48 ms
57,172 KB |
testcase_05 | AC | 47 ms
56,684 KB |
testcase_06 | AC | 48 ms
57,448 KB |
testcase_07 | AC | 147 ms
78,824 KB |
testcase_08 | AC | 67 ms
71,080 KB |
testcase_09 | AC | 65 ms
68,876 KB |
testcase_10 | AC | 3,041 ms
179,492 KB |
testcase_11 | AC | 3,209 ms
179,468 KB |
testcase_12 | AC | 3,006 ms
178,240 KB |
testcase_13 | AC | 3,045 ms
178,588 KB |
testcase_14 | AC | 3,158 ms
179,844 KB |
testcase_15 | AC | 3,188 ms
179,520 KB |
testcase_16 | AC | 1,334 ms
116,264 KB |
testcase_17 | AC | 2,375 ms
156,408 KB |
testcase_18 | AC | 47 ms
57,524 KB |
testcase_19 | AC | 48 ms
56,512 KB |
testcase_20 | AC | 47 ms
57,536 KB |
testcase_21 | AC | 48 ms
58,204 KB |
testcase_22 | AC | 49 ms
57,572 KB |
testcase_23 | AC | 50 ms
58,012 KB |
testcase_24 | AC | 48 ms
57,084 KB |
testcase_25 | AC | 48 ms
58,236 KB |
testcase_26 | AC | 2,872 ms
179,488 KB |
testcase_27 | AC | 1,421 ms
178,844 KB |
testcase_28 | AC | 1,430 ms
178,972 KB |
testcase_29 | AC | 318 ms
82,576 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)