QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#299188#6300. Best Carry Player 2wy#WA 0ms3808kbC++141.9kb2024-01-06 17:36:002024-01-06 17:36:01

Judging History

This is the latest submission verdict.

  • [2024-01-06 17:36:01]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3808kb
  • [2024-01-06 17:36:00]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
using i128 = __int128;

const i128 N = 40, INF = 0x3f3f3f3f3f3f3f3f;

int n, m;
i128 dp[N][20][2];
int a[N];
i128 pw[N];

void print(i128 x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9)
        print(x / 10);
    putchar('0' + x % 10);
}

void solve()
{
    string s;
    cin >> s >> m;
    reverse(s.begin(), s.end());

    if (!m)
    {
        for (int i = 0; i < s.size(); i++)
            if (s[i] != '9')
            {
                print(pw[i]);
                puts("");
                return;
            }
        print(pw[s.size()]);
        puts("");
        return;
    }

    n = s.size() + m - 1;
    for (int i = 1; i <= s.size(); i++)
        a[i] = s[i - 1] - '0';

    memset(dp, 0x3f, sizeof dp);
    dp[0][0][0] = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= i && j <= m; j++)
        {
            if (j)
            {
                if (a[i] != 0)
                    dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][0] + (10 - a[i]) * pw[i - 1]);
                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][1] + (9 - a[i]) * pw[i - 1]);
            }
            dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j][0]);
            if (a[i] != 9)
                dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j][1]);
        }

    i128 ans = INF;
    for (int i = 1; i <= n; i++)
    {
        ans = min(ans, dp[i][m][0]);
        if (a[i + 1] != 9)
            ans = min(ans, dp[i][m][1]);
    }

    print(ans);
    puts("");
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    pw[0] = 1;
    for (int i = 1; i < N; i++)
        pw[i] = pw[i - 1] * 10;

    int T;
    cin >> T;
    while (T--)
        solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3548kb

input:

4
12345678 0
12345678 5
12345678 18
990099 5

output:

1
54322
999999999987654322
9910

result:

ok 4 lines

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3808kb

input:

21
999990000099999 0
999990000099999 1
999990000099999 2
999990000099999 3
999990000099999 4
999990000099999 5
999990000099999 6
999990000099999 7
999990000099999 8
999990000099999 9
999990000099999 10
999990000099999 11
999990000099999 12
999990000099999 13
999990000099999 14
999990000099999 15
999...

output:

100000
10000
1000
100
10
1
900001
9900001
99900001
999900001
10000000001
9999910000
9999901000
9999900100
9999900010
9999900001
9000009999900001
99000009999900001
999000009999900001
4557430888798830399
1000000000000000000

result:

wrong answer 20th lines differ - expected: '99999999999999999900000000000000000', found: '4557430888798830399'