QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#600416#6300. Best Carry Player 2ucup-team3691#WA 1119ms3724kbC++143.1kb2024-09-29 16:23:452024-09-29 16:23:45

Judging History

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

  • [2024-09-29 16:23:45]
  • 评测
  • 测评结果:WA
  • 用时:1119ms
  • 内存:3724kb
  • [2024-09-29 16:23:45]
  • 提交

answer

#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <random>
#include <ctime>
#include <cstdlib>
#include <chrono>

using namespace std;

using ll = long long;

struct usu {
  int c, i, j, t;
};

void solve() {
  ll x;
  int k;
  cin >> x >> k;

  if (k == 0) {
    ll p = 1;
    while (x > 0) {
      if (x % 10 != 9) {
        cout << p << "\n";
        return;
      }
      p = p * 10;
      x /= 10;
    }

    cout << p << '\n';
    return;
  }

  ll off = 1, offk = 0;
  while (x % 10 == 0) {
    off *= 10;
    x /= 10;
    ++offk;
  }

  vector<int> cif;
  cif.push_back(0);
  while (x > 0) {
    cif.push_back(x % 10);
    x /= 10;
  }

  int n = cif.size();
  
  if (k > n - 1) {
    bool fs = true;
    string ans;

    while (offk--) {
      ans.push_back('0');
    }

    for (int i = 1; i < n; ++i) {
      if (fs && cif[i] == 0) {
        ans.push_back('0');
        continue;
      }

      --k;
      ans.push_back('0' + 10 - cif[i] - 1 + fs);
      fs = false;
    }

    while (k--) {
      ans.push_back('9');
    }

    reverse(ans.begin(), ans.end());
    cout << ans << "\n";
    return;
  }
  
  vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(k + 1, vector<int>(2, -1)));
  vector<vector<vector<usu>>> tt(n + 1, vector<vector<usu>>(k + 1, vector<usu>(2)));

  dp[1][0][0] = 0;

  vector<ll> p10(n + 1, 1);
  for (int i = 1; i < n; ++i) {
    if (i > 1) {
      p10[i] = p10[i - 1] * 10;
    }

    int r = 0, nxt = i + 1;
    while (nxt < n && cif[nxt] == 9) {
      ++r;
      ++nxt;
    }

    for (int j = 0; j <= k; ++j) {
      if (dp[i][j][0] == -1) {
        continue;
      }

      if (dp[i + 1][j][0] == -1) {
        dp[i + 1][j][0] = 1; 
        tt[i + 1][j][0] = {0, i, j, 0};
      }

      if (cif[i] == 0 || j + 1 + r > k) {
        continue;
      }

      if (dp[nxt][j + 1 + r][1] == -1) {
        dp[nxt][j + 1 + r][1] = 1; 
        tt[nxt][j + 1 + r][1] = {10 - cif[i], i, j, 0};
      }
    }

    for (int j = 0; j <= k; ++j) {
      if (dp[i][j][1] == -1) {
        continue;
      }

      if (dp[i + 1][j][0] == -1) {
        dp[i + 1][j][0] = 1; 
        tt[i + 1][j][0] = {0, i, j, 1};
      }

      if (j + r + 1 > k) {
        continue;
      }

      if (dp[nxt][j + r + 1][1] == -1) {
        dp[nxt][j + r + 1][1] = 1; 
        tt[nxt][j + r + 1][1] = {10 - cif[i] - 1, i, j, 1};
      }
    }
  }

  if (dp[n][k][0] == -1 && dp[n][k][1] == -1) {
    cout << "-1\n";
    return;
  }

  ll ans = 0;
  for (int i = n, j = k, t = (dp[n][k][0] == 1) ? 0 : 1; i > 0;) {
    auto nxt = tt[i][j][t];
    ans = ans + p10[nxt.i] * nxt.c;
    tie(i, j, t) = make_tuple(nxt.i, nxt.j, nxt.t);
  }

  ans *= off;

  cout << ans << "\n";
}

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

  int t = 1;
  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: 3540kb

input:

4
12345678 0
12345678 5
12345678 18
990099 5

output:

1
54322
999999999987654322
9910

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 3544kb

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
99999999999999999900000000000000000
1000000000000000000

result:

ok 21 lines

Test #3:

score: -100
Wrong Answer
time: 1119ms
memory: 3724kb

input:

100000
119111011091190000 10
1911011191011999 16
110099199000119 0
19009911191091011 13
199090909919000900 17
19009010011919110 5
90910190019900091 18
10911100000101111 1
110090011101119990 4
100909999119090000 12
90901119109011100 2
111010119991090101 4
900991019109199009 5
100919919990991119 8
911...

output:

88988908810000
8088988808988001
10
88808908989
9800909090080999100
80890
909089809980099909
9
80010
9090000880910000
8900
9909
991
9009001
9080880081
8080090801
8009900808909899
80880898981
909
8800909
99988889901
89908888089
980908890980099000
100
9889801
81
908890008099900891
880990801
9998099
890...

result:

wrong answer 14th lines differ - expected: '9008900', found: '9009001'