#include <iostream>
#include <vector>
using namespace std;
int compare_as_int(string& a, string& b) {
if (a.size() < b.size())
return -1;
if (a.size() > b.size())
return 1;
for (int i = 0; i < b.size(); i++) {
if ((a[i] - '0') < (b[i] - '0'))
return -1;
if ((a[i] - '0') > (b[i] - '0'))
return 1;
}
return 0;
}
int compare_front(string& a, string& b) {
for (int i = 0; i < a.size(); ++i) {
if ((a[i] - '0') < (b[i] - '0'))
return -1;
if ((a[i] - '0') > (b[i] - '0'))
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
string a, b;
cin >> a;
long result = 0;
for (int i = 1; i < n; ++i) {
cin >> b;
if (compare_as_int(b, a) <= 0) {
int res = compare_front(b, a);
if (res == 1) {
result += a.size() - b.size();
b += string(a.size() - b.size(), '0');
} else if (res == -1) {
result += a.size() - b.size() + 1;
b += string(a.size() - b.size() + 1, '0');
} else {
int nnp = -1;
for (int k = a.size() - 1; k >= b.size(); --k) {
if (a[k] != '9') {
nnp = k;
break;
}
}
if (nnp == -1) {
result += a.size() - b.size() + 1;
b += string(a.size() - b.size() + 1, '0');
} else {
result += a.size() - b.size();
b += a.substr(b.size(), nnp - b.size()) + string(1, a[nnp] + 1) + string(a.size() - nnp - 1, '0');
}
}
}
a = move(b);
}
/*
for (auto e : v)
cout << e << endl;
cout << endl;
*/
cout << result << endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <iostream> #include <vector> using namespace std; int compare_as_int(string& a, string& b) { if (a.size() < b.size()) return -1; if (a.size() > b.size()) return 1; for (int i = 0; i < b.size(); i++) { if ((a[i] - '0') < (b[i] - '0')) return -1; if ((a[i] - '0') > (b[i] - '0')) return 1; } return 0; } int compare_front(string& a, string& b) { for (int i = 0; i < a.size(); ++i) { if ((a[i] - '0') < (b[i] - '0')) return -1; if ((a[i] - '0') > (b[i] - '0')) return 1; } return 0; } int main() { ios::sync_with_stdio(false); int n; cin >> n; string a, b; cin >> a; long result = 0; for (int i = 1; i < n; ++i) { cin >> b; if (compare_as_int(b, a) <= 0) { int res = compare_front(b, a); if (res == 1) { result += a.size() - b.size(); b += string(a.size() - b.size(), '0'); } else if (res == -1) { result += a.size() - b.size() + 1; b += string(a.size() - b.size() + 1, '0'); } else { int nnp = -1; for (int k = a.size() - 1; k >= b.size(); --k) { if (a[k] != '9') { nnp = k; break; } } if (nnp == -1) { result += a.size() - b.size() + 1; b += string(a.size() - b.size() + 1, '0'); } else { result += a.size() - b.size(); b += a.substr(b.size(), nnp - b.size()) + string(1, a[nnp] + 1) + string(a.size() - nnp - 1, '0'); } } } a = move(b); } /* for (auto e : v) cout << e << endl; cout << endl; */ cout << result << endl; return 0; } |
English