QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#779073 | #6300. Best Carry Player 2 | Anneliese# | WA | 7ms | 10764kb | Python3 | 2.0kb | 2024-11-24 17:20:43 | 2024-11-24 17:20:44 |
Judging History
answer
import sys
import math
# Optimized version of the solve function to reduce time complexity
def solve(x, m):
# Precompute all necessary powers of 10 to avoid redundant recalculations
num = [1] * 40
for i in range(1, 37):
num[i] = num[i - 1] * 10
# Convert x into its digit array in reverse order
a = []
while x > 0:
a.append(x % 10)
x //= 10
cnt = len(a)
# If x is 0, we have one digit
if cnt == 0:
a.append(0)
cnt = 1
# Base case for m == 0, we check for the first non-9 digit
if m == 0:
for i in range(cnt):
if a[i] != 9:
print(num[i])
return
# Use a dynamic programming array to track minimum costs
dp = [[float('inf')] * (m + 1) for _ in range(2)]
dp[0][0] = 0
# Iterate over each digit position
for i in range(cnt):
current = i % 2
next_ = (i + 1) % 2
dp[next_] = [float('inf')] * (m + 1)
for k in range(m + 1):
# No carry case
if a[i] + 1 < 10:
dp[next_][k] = min(dp[next_][k], dp[current][k])
else:
dp[next_][k] = min(dp[next_][k], dp[current][k])
# Carry case, only if we still have carries left
if k < m:
if a[i] != 0:
dp[next_][k + 1] = min(dp[next_][k + 1], (10 - a[i]) * num[i] + dp[current][k])
else:
dp[next_][k + 1] = min(dp[next_][k + 1], (10 - a[i] - 1) * num[i] + dp[current][k])
ans = float('inf')
for i in range(m + 1):
ans = min(ans, dp[cnt % 2][i])
print(ans)
return
def main():
input = sys.stdin.read
data = input().split()
t = int(data[0])
idx = 1
results = []
for _ in range(t):
x = int(data[idx])
m = int(data[idx + 1])
idx += 2
solve(x, m)
if __name__ == "__main__":
main()
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 10764kb
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
1 0 0 0
result:
wrong answer 2nd lines differ - expected: '54322', found: '0'