#include <iostream> #include <cstring> using namespace std; // #define DEBUG #ifdef DEBUG #define PREFIX cerr << __FUNCTION__ << ": " #define PRINT(x) #x " = " << (x) #define PRINT1(x) PREFIX << PRINT(x) << endl #define PRINT2(x, y) PREFIX << PRINT(x) << ", " << PRINT(y) << endl #define PRINT3(x, y, z) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << endl #define PRINT4(x, y, z, t) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << ", " << PRINT(t) << endl #else #define PRINT1(x) #define PRINT2(x, y) #define PRINT3(x, y, z) #define PRINT4(x, y, z, t) #endif struct Number { static const int maxLen = 25; char bytes[maxLen + 1]; int length; void adjust(const Number &other); int diffPos(const Number &other) const; bool allNines(int startPos) const; void appendZeros(int startPos); void copy(const Number &other); void addOne(); }; const int Number::maxLen; istream &operator>>(istream &in, Number &num) { in >> num.bytes; num.length = strlen(num.bytes); return in; } ostream &operator<<(ostream &out, const Number &num) { out << '(' << num.length << ' ' << num.bytes << ')'; return out; } int Number::diffPos(const Number &other) const { for (int i = 0; bytes[i] and other.bytes[i]; ++i) { if (bytes[i] != other.bytes[i]) return i; } return -1; } bool Number::allNines(int startPos) const { for (int i = startPos; bytes[i]; ++i) { if (bytes[i] != '9') return false; } return true; } void Number::appendZeros(int howMany) { PRINT2(*this, howMany); for (int i = length; i < min(maxLen, length + howMany); ++i) { bytes[i] = '0'; bytes[i + 1] = '\0'; } length += howMany; } void Number::copy(const Number &other) { int i = length; for (; other.bytes[i]; ++i) { bytes[i] = other.bytes[i]; } bytes[i] = '\0'; length = other.length; } void Number::addOne() { // assumption: not all digits are nine for (int i = length - 1;; --i) { if (bytes[i] != 9) { ++bytes[i]; return; } else { bytes[i] = 0; } } } void Number::adjust(const Number &other) { PRINT2(*this, other); if (other.length < length) return; int diff = diffPos(other); PRINT1(diff); if (other.length == length) { if (diff == -1) { // equal, append zero appendZeros(1); return; } if (other.bytes[diff] < bytes[diff]) return; // I am greater, done appendZeros(1); // I am smaller, append zero } if (other.length > length) { if (diff == -1) { // equal prefixes if (other.allNines(length)) { appendZeros(other.length - length + 1); } else { copy(other); addOne(); } } else { if (other.bytes[diff] < bytes[diff]) { appendZeros(other.length - length); } else { appendZeros(other.length - length + 1); } } } } int main() { int n; long long total = 0; Number numbers[2]; cin >> n; cin >> numbers[0]; for (int i = 1; i < n; ++i) { int current = i & 1; cin >> numbers[current]; int len = numbers[current].length; numbers[current].adjust(numbers[1 - current]); total += numbers[current].length - len; PRINT3(i, numbers[current], total); } cout << total << endl; }
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #include <iostream> #include <cstring> using namespace std; // #define DEBUG #ifdef DEBUG #define PREFIX cerr << __FUNCTION__ << ": " #define PRINT(x) #x " = " << (x) #define PRINT1(x) PREFIX << PRINT(x) << endl #define PRINT2(x, y) PREFIX << PRINT(x) << ", " << PRINT(y) << endl #define PRINT3(x, y, z) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << endl #define PRINT4(x, y, z, t) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << ", " << PRINT(t) << endl #else #define PRINT1(x) #define PRINT2(x, y) #define PRINT3(x, y, z) #define PRINT4(x, y, z, t) #endif struct Number { static const int maxLen = 25; char bytes[maxLen + 1]; int length; void adjust(const Number &other); int diffPos(const Number &other) const; bool allNines(int startPos) const; void appendZeros(int startPos); void copy(const Number &other); void addOne(); }; const int Number::maxLen; istream &operator>>(istream &in, Number &num) { in >> num.bytes; num.length = strlen(num.bytes); return in; } ostream &operator<<(ostream &out, const Number &num) { out << '(' << num.length << ' ' << num.bytes << ')'; return out; } int Number::diffPos(const Number &other) const { for (int i = 0; bytes[i] and other.bytes[i]; ++i) { if (bytes[i] != other.bytes[i]) return i; } return -1; } bool Number::allNines(int startPos) const { for (int i = startPos; bytes[i]; ++i) { if (bytes[i] != '9') return false; } return true; } void Number::appendZeros(int howMany) { PRINT2(*this, howMany); for (int i = length; i < min(maxLen, length + howMany); ++i) { bytes[i] = '0'; bytes[i + 1] = '\0'; } length += howMany; } void Number::copy(const Number &other) { int i = length; for (; other.bytes[i]; ++i) { bytes[i] = other.bytes[i]; } bytes[i] = '\0'; length = other.length; } void Number::addOne() { // assumption: not all digits are nine for (int i = length - 1;; --i) { if (bytes[i] != 9) { ++bytes[i]; return; } else { bytes[i] = 0; } } } void Number::adjust(const Number &other) { PRINT2(*this, other); if (other.length < length) return; int diff = diffPos(other); PRINT1(diff); if (other.length == length) { if (diff == -1) { // equal, append zero appendZeros(1); return; } if (other.bytes[diff] < bytes[diff]) return; // I am greater, done appendZeros(1); // I am smaller, append zero } if (other.length > length) { if (diff == -1) { // equal prefixes if (other.allNines(length)) { appendZeros(other.length - length + 1); } else { copy(other); addOne(); } } else { if (other.bytes[diff] < bytes[diff]) { appendZeros(other.length - length); } else { appendZeros(other.length - length + 1); } } } } int main() { int n; long long total = 0; Number numbers[2]; cin >> n; cin >> numbers[0]; for (int i = 1; i < n; ++i) { int current = i & 1; cin >> numbers[current]; int len = numbers[current].length; numbers[current].adjust(numbers[1 - current]); total += numbers[current].length - len; PRINT3(i, numbers[current], total); } cout << total << endl; } |