#include <bits/stdc++.h> unsigned int min(unsigned int a, unsigned int b) { if (a < b) return a; return b; } int compare_string(const std::string& left, const std::string&right) { if (left.size() > right.size()) return 1; if (right.size() > left.size()) return -1; for (int i = 0; i < left.size(); i++) { if (left[i] > right[i]) return 1; if (left[i] < right[i]) return -1; } return 0; } int compare_string_on_prefix(const std::string& left, const std::string&right) { for (int i = 0; i < min(left.size(), right.size()); i++) { if (left[i] > right[i]) return 1; if (left[i] < right[i]) return -1; } return 0; } std::vector<std::string> read() { int n; std::cin >> n; std::vector<std::string> result(n); for (int i = 0; i < n; i++) std::cin >> result[i]; return result; } int add_digits(std::string& a, std::string& b) { if (compare_string(a, b) == -1) return 0; int cmp = compare_string_on_prefix(a, b); if (cmp == 0) { int last = -1; int result = 0; int old_size = b.size(); while (a.size() > b.size()) { if (a[b.size()] != '9') last = b.size(); b.push_back(a[b.size()]); result++; } if (last != -1) { b[last]++; last++; for (int i = last; i < b.size(); i++) b[i] = '0'; } else { for (int i = old_size; i < b.size(); i++) { b[i] = '0'; } b.push_back('0'); return result + 1; } return result; } else if (cmp == -1) { int result = a.size() - b.size(); while (a.size() > b.size()) b.push_back('0'); return result; } else { int result = a.size() - b.size() + 1; while (a.size() > b.size()) b.push_back('0'); b.push_back('0'); return result; } } int solve(std::vector<std::string>& numbers) { long long result = 0; for (int i = 1; i < numbers.size(); i++) { result += add_digits(numbers[i - 1], numbers[i]); } return result; } int main() { std::ios_base::sync_with_stdio(false); std::vector<std::string> numbers = read(); std::cout << solve(numbers) << std::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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include <bits/stdc++.h> unsigned int min(unsigned int a, unsigned int b) { if (a < b) return a; return b; } int compare_string(const std::string& left, const std::string&right) { if (left.size() > right.size()) return 1; if (right.size() > left.size()) return -1; for (int i = 0; i < left.size(); i++) { if (left[i] > right[i]) return 1; if (left[i] < right[i]) return -1; } return 0; } int compare_string_on_prefix(const std::string& left, const std::string&right) { for (int i = 0; i < min(left.size(), right.size()); i++) { if (left[i] > right[i]) return 1; if (left[i] < right[i]) return -1; } return 0; } std::vector<std::string> read() { int n; std::cin >> n; std::vector<std::string> result(n); for (int i = 0; i < n; i++) std::cin >> result[i]; return result; } int add_digits(std::string& a, std::string& b) { if (compare_string(a, b) == -1) return 0; int cmp = compare_string_on_prefix(a, b); if (cmp == 0) { int last = -1; int result = 0; int old_size = b.size(); while (a.size() > b.size()) { if (a[b.size()] != '9') last = b.size(); b.push_back(a[b.size()]); result++; } if (last != -1) { b[last]++; last++; for (int i = last; i < b.size(); i++) b[i] = '0'; } else { for (int i = old_size; i < b.size(); i++) { b[i] = '0'; } b.push_back('0'); return result + 1; } return result; } else if (cmp == -1) { int result = a.size() - b.size(); while (a.size() > b.size()) b.push_back('0'); return result; } else { int result = a.size() - b.size() + 1; while (a.size() > b.size()) b.push_back('0'); b.push_back('0'); return result; } } int solve(std::vector<std::string>& numbers) { long long result = 0; for (int i = 1; i < numbers.size(); i++) { result += add_digits(numbers[i - 1], numbers[i]); } return result; } int main() { std::ios_base::sync_with_stdio(false); std::vector<std::string> numbers = read(); std::cout << solve(numbers) << std::endl; return 0; } |