QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#332135#6300. Best Carry Player 2martinius#WA 1ms3624kbC++202.2kb2024-02-19 09:57:052024-02-19 09:57:06

Judging History

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

  • [2024-02-19 09:57:06]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3624kb
  • [2024-02-19 09:57:05]
  • 提交

answer

#include <bits/stdc++.h>

#define sz(v) v.size()
#define forn(i,n) for(int i = 0; i < n; ++i)
#define fi first
#define se second
#define el '\n'
#define all(v) v.begin(),v.end()
#define pb push_back
#define d(x) cout << #x << " : " << x << el

using namespace std;

typedef pair<int,int> ii;

string s; 
int k;

const int nax = 19;
ii dp[nax][nax][2];

ii go(int i, int sum, bool carry){
  int num = 0;
  if(i < sz(s)) num = s[i] - '0';
  if(i >= nax) return {0, 0};
  if(sum == k){
    num += carry;
    return {num <= 9, i};
  }
  ii &r = dp[i][sum][carry];
  if(r.fi!=-1) return r;
  r = {0, 1e9};

  forn(dd,10){
    int val = num + carry + dd;
    ii cur;
    if(val >= 10){
      cur = go(i + 1, sum + 1, 1);
    }else{
      cur = go(i + 1, sum, 0);
    }
    if(cur.fi){
      if(r.fi == 0) r = cur;
      else r = {1, min(cur.se, r.se)};
    }
  }
  return r;
}

string ans;


void build(int i, int sum, bool carry){
  int num = 0;
  if(i < sz(s)) num = s[i] - '0';

  if(sum == k){
    return ;
  }
  ii &r = dp[i][sum][carry];
  forn(dd,10){
    int val = num + carry + dd;
    ii cur;
    if(val >= 10){
      if(r == go(i + 1, sum + 1, 1)){
        ans.pb(dd+'0');
        build(i + 1, sum + 1, 1);
        return ;
      }
    }else{
      if(r == go(i + 1, sum, 0)){
        ans.pb(dd+'0');
        build(i + 1, sum, 0);
        return ;
      }
    }
  }
}


int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  cout << setprecision(20)<< fixed;

  int t;
  cin >> t;
  while(t--){
    cin >> s;
    cin >> k;
    reverse(all(s));

    if(k == 0){
      int cnt = 0;
      for(char c : s){
        if(c!='9') break;
        ++cnt;
      }
      cout << "1" + string(cnt, '0') << el;
      continue;
    }
    forn(i,nax){
      forn(j,nax){
        forn(j2,2){
          dp[i][j][j2] = {-1,-1};
        }
      }
    }
    ii cur = go(0, 0, 0);
    if(cur.fi == 0){
      cout << -1 << el;
      continue;
    }
    build(0, 0, 0);
    while(ans.back()=='0') ans.pop_back();
    reverse(all(ans));
    cout << ans << el;

    ans.clear();
  }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3612kb

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: 1ms
memory: 3624kb

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
10999910000
9999910000
9999901000
9999900100
9999900010
9999900001
9000009999900001
99000009999900001
999000009999900001
-1
1000000000000000000

result:

wrong answer 11th lines differ - expected: '10000000001', found: '10999910000'