QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#779034#6300. Best Carry Player 2wangshengzheRE 0ms0kbPython31.5kb2024-11-24 17:09:352024-11-24 17:09:36

Judging History

你现在查看的是最新测评结果

  • [2024-11-24 17:09:36]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-24 17:09:35]
  • 提交

answer

import sys
import math


def read():
    return int(input().strip())


def print_ans(n):
    if n < 0:
        print('-', end='')
        n = -n
    print(n)


def solve(x, m):
    dp = [[[float('inf')] * 2 for _ in range(20)] for _ in range(40)]
    a = [0] * 20
    cnt = 0
    while x:
        cnt += 1
        a[cnt] = x % 10
        x //= 10

    num = [1] * 40
    for i in range(1, 37):
        num[i] = num[i - 1] * 10

    dp[0][0][0] = 0
    p = max(cnt, m)

    if m == 0:
        for i in range(1, cnt + 2):
            if a[i] != 9:
                print_ans(num[i - 1])
                return

    for i in range(1, 37):
        for k in range(0, m + 1):
            if a[i] + 1 < 10:
                dp[i][k][0] = min(dp[i - 1][k][1], dp[i - 1][k][0])
            else:
                dp[i][k][0] = dp[i - 1][k][0]

            if k != 0:
                if a[i] != 0:
                    dp[i][k][1] = min((10 - a[i] - 1) * num[i - 1] + dp[i - 1][k - 1][1],
                                      (10 - a[i]) * num[i - 1] + dp[i - 1][k - 1][0])
                else:
                    dp[i][k][1] = (10 - a[i] - 1) * num[i - 1] + dp[i - 1][k - 1][1]

    ans = float('inf')
    for i in range(cnt, 37):
        ans = min(ans, min(dp[i][m][1], dp[i][m][0]))

    print_ans(ans)
    return


def main():
    t = int(input())
    for _ in range(t):
        (x, m) = map(int, input().split())
        solve(x, m)


if __name__ == "__main__":
    main()

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

4
12345678 0
12345678 5
12345678 18
990099 5

output:

1

result: