Home Article Practice codeforces.com/contest/1923/problem/D

codeforces.com/contest/1923/problem/D

2024-02-24 13:11  views:154  source:白可    

import sys
from collections import deque
input = sys.stdin.readline
def iinput(): return int(input())
def linput(): return list(map(int, input().split()))
mod = 998244353
def solve(L):
n = len(L)
psum = [0]
res = [-1] * n
for i in L: psum.append(psum[-1] + i)
rL = [0] * len(L)
rL[-1] = len(L) - 1
for i in range(len(L) - 2, -1, -1):
if L[i] == L[i + 1]:
rL[i] = rL[i + 1]
else:
rL[i] = i
# print(rL)
for i in range(n):
if psum[len(L)] - psum[i + 1] <= L[i]: continue
if L[i + 1] <= L[i] and rL[i + 1] == n - 1: continue
start = rL[i] + 1
if i < n - 1 and L[i + 1] <= L[i]: start = rL[i + 1] + 1
end = n - 1
while start < end:
mid = (start + end) // 2
tot = psum[mid + 1] - psum[i + 1]
if tot > L[i]:
end = mid
else:
start = mid + 1
res[i] = start - i
return res
for _ in ' ' * iinput():
n = iinput()
L = linput()
res1 = solve(L)
res2 = solve(L[::-1])[::-1]
res = []
for i in range(n):
if res1[i] == -1:
res.append(res2[i])
elif res2[i] == -1:
res.append(res1[i])
else:
res.append(min(res1[i], res2[i]))
for i in res: print(i, end=' ')
print()



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)