#include <bits/stdc++.h> using namespace std; class Number { public: Number(string head_) : head(head_), zeros(0) { normalize(); } Number(string head_, int zeros_) : head(head_), zeros(zeros_) { normalize(); } Number(string head_, int zeros_, string tail_) : head(head_), zeros(zeros_), tail(tail_) { normalize(); } int getLength() const { return head.length() + zeros; } char getIth(int i) const { assert (i >= 0 && i < getLength()); if (i < head.length()) { return head[i]; } if (i >= getLength() - tail.length()) { return tail[i - getLength() + tail.length()]; } return '0'; } void normalize() { while (head.back() == '0') { head.pop_back(); zeros++; } if (tail.length() > zeros) { tail.clear(); zeros++; head = to_string(stoll(head) + 1); } while (head.back() == '0') { head.pop_back(); zeros++; } if (zeros == tail.size()) { head = head + tail; zeros = 0; tail = ""; } while (head.back() == '0') { head.pop_back(); zeros++; } } bool operator < (const string& other) { if (this->getLength() == other.length()) { int l = other.length(); for (int i = 0; i < l; i++) { if (this->getIth(i) != other[i]) { return this->getIth(i) < other[i]; } } return false; } return this->getLength() < other.length(); } string head; int zeros; string tail; }; Number increment(const Number& number) { if (number.zeros == 0) { return Number(to_string(stoll(number.head) + 1)); } else { if (number.tail.empty()) { return Number(number.head, number.zeros, "1"); } return Number(number.head, number.zeros, to_string(stoll(number.tail) + 1)); } } Number findBigger(Number from, string newNumber) { if (from < newNumber) { return Number(newNumber); } if (newNumber.length() == from.getLength()) { return Number(newNumber, 1); } int newLength = newNumber.length(); int fromLength = from.getLength(); for (int i = 0; i < newLength; i++) { if (newNumber[i] > from.getIth(i)) { return Number(newNumber, fromLength - newLength); } if (newNumber[i] < from.getIth(i)) { return Number(newNumber, fromLength - newLength + 1); } } for (int i = newLength; i < fromLength; i++) { if (from.getIth(i) != '9') { return increment(from); } } return Number(newNumber, fromLength - newLength + 1); } int main() { long long result = 0; int n; cin >> n; Number last = Number(""); while (n--) { int a; cin >> a; string s = to_string(a); last = findBigger(last, s); result += last.getLength() - s.length(); } 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 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 | #include <bits/stdc++.h> using namespace std; class Number { public: Number(string head_) : head(head_), zeros(0) { normalize(); } Number(string head_, int zeros_) : head(head_), zeros(zeros_) { normalize(); } Number(string head_, int zeros_, string tail_) : head(head_), zeros(zeros_), tail(tail_) { normalize(); } int getLength() const { return head.length() + zeros; } char getIth(int i) const { assert (i >= 0 && i < getLength()); if (i < head.length()) { return head[i]; } if (i >= getLength() - tail.length()) { return tail[i - getLength() + tail.length()]; } return '0'; } void normalize() { while (head.back() == '0') { head.pop_back(); zeros++; } if (tail.length() > zeros) { tail.clear(); zeros++; head = to_string(stoll(head) + 1); } while (head.back() == '0') { head.pop_back(); zeros++; } if (zeros == tail.size()) { head = head + tail; zeros = 0; tail = ""; } while (head.back() == '0') { head.pop_back(); zeros++; } } bool operator < (const string& other) { if (this->getLength() == other.length()) { int l = other.length(); for (int i = 0; i < l; i++) { if (this->getIth(i) != other[i]) { return this->getIth(i) < other[i]; } } return false; } return this->getLength() < other.length(); } string head; int zeros; string tail; }; Number increment(const Number& number) { if (number.zeros == 0) { return Number(to_string(stoll(number.head) + 1)); } else { if (number.tail.empty()) { return Number(number.head, number.zeros, "1"); } return Number(number.head, number.zeros, to_string(stoll(number.tail) + 1)); } } Number findBigger(Number from, string newNumber) { if (from < newNumber) { return Number(newNumber); } if (newNumber.length() == from.getLength()) { return Number(newNumber, 1); } int newLength = newNumber.length(); int fromLength = from.getLength(); for (int i = 0; i < newLength; i++) { if (newNumber[i] > from.getIth(i)) { return Number(newNumber, fromLength - newLength); } if (newNumber[i] < from.getIth(i)) { return Number(newNumber, fromLength - newLength + 1); } } for (int i = newLength; i < fromLength; i++) { if (from.getIth(i) != '9') { return increment(from); } } return Number(newNumber, fromLength - newLength + 1); } int main() { long long result = 0; int n; cin >> n; Number last = Number(""); while (n--) { int a; cin >> a; string s = to_string(a); last = findBigger(last, s); result += last.getLength() - s.length(); } cout << result << endl; return 0; } |