QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#165793 | #6300. Best Carry Player 2 | 8xin | WA | 2ms | 3764kb | C++14 | 2.5kb | 2023-09-05 21:46:14 | 2023-09-05 21:46:14 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
__int128 wei[39];
__int128 dp0[40][40];//该位不接受低位进位的情况下,dp[i][j]前i位进了j个位最小值是多少,若不存在则为0
__int128 dp1[40][40];//该位接受低位进位的情况下,dp[i][j]前i位进了j个位最小值是多少
void print(__int128 x) {
stack<int>st;
while (x > 0) {
st.push(x % 10);
x /= 10;
}
while (st.size()) {
cout << st.top();
st.pop();
}
cout << endl;
}
void solve() {
string str, tem;
int K, lim;
cin >> str >> K;
for (int i = 1; i < K; i ++) {
tem += '0';
}
str = tem + str;
str = '?' + str;
int siz = str.size() - 1;
for (int i = 1; i <= siz; i ++) {
for (int j = 0; j <= K; j ++) {
dp0[i][j] = -1;
dp1[i][j] = -1;
}
}
dp0[1][0] = 0;
if (str[1] == '0') {
dp0[1][1] = -1;
} else {
dp0[1][1] = wei[siz];
dp0[1][1] *= 10 - str[1] + '0';
}
if (str[1] == '9') {
dp1[1][0] = -1;
dp1[1][1] = 0;
} else {
dp1[1][0] = 0;
dp1[1][1] = wei[siz];
dp1[1][1] *= 9 - str[1] + '0';
}
for (int i = 2; i <= siz; i ++) {
for (int j = 0; j <= i and j <= K; j ++) {
//dp0[i][j] = ?;
if (str[i] == '0') {
dp0[i][j] = dp0[i - 1][j];
} else {
if (dp0[i - 1][j] == -1) {
if (j == 0 or dp1[i - 1][j - 1] == -1) {
dp0[i][j] = -1;
} else {
dp0[i][j] = dp1[i - 1][j - 1] + wei[siz - i + 1] * (10 - str[i] + '0');
}
} else {
if (j == 0 or dp1[i - 1][j - 1] == -1) {
dp0[i][j] = dp0[i - 1][j];
} else {
dp0[i][j] = min(dp0[i - 1][j], dp1[i - 1][j - 1] + wei[siz - i + 1] * (10 - str[i] + '0'));
}
}
}
//dp1[i][j] = ?;
if (str[i] == '9') {
if (j == 0) {
dp1[i][j] = -1;
} else {
dp1[i][j] = dp1[i - 1][j - 1];
}
} else {
if (dp0[i - 1][j] == -1) {
if (j == 0 or dp1[i - 1][j - 1] == -1) {
dp1[i][j] = -1;
} else {
dp1[i][j] = dp1[i - 1][j - 1] + wei[siz - i + 1] * (9 - str[i] + '0');
}
} else {
if (j == 0 or dp1[i - 1][j - 1] == -1) {
dp1[i][j] = dp0[i - 1][j];
} else {
dp1[i][j] = min(dp0[i - 1][j], dp1[i - 1][j - 1] + wei[siz - i + 1] * (9 - str[i] + '0'));
}
}
}
}
}
if (dp0[siz][K] == 0) {
dp0[siz][K] = 1;
}
print(dp0[siz][K]);
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int T;
cin >> T;
wei[1] = 1;
for (int i = 2; i <= 38; i ++) {
wei[i] = wei[i - 1] * 10;
}
while (T --) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3764kb
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: 2ms
memory: 3644kb
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:
1 10000 1000 100 10 1 900001 9900001 99900001 999900001 10000000001 9999910000 9999901000 9999900100 9999900010 9999900001 9000009999900001 99000009999900001 999000009999900001 99999999999999999900000000000000000 1
result:
wrong answer 1st lines differ - expected: '100000', found: '1'