#include <cstdio> #include <algorithm> #include <vector> #include <stack> #include <string> #include <iostream> #include <cmath> std::string Buffer; void FillNumberV(std::vector<char>& v, long long integer) { v.clear(); std::stack<char> s; while (integer) { s.push(integer % 10); integer /= 10; } while (s.size()) v.push_back(s.top()), s.pop(); } std::vector<char> FillNumber(long long integer) { std::vector<char> v; v.reserve(16); FillNumberV(v, integer); return v; } struct Number { long long Integer = 0; std::vector<char> Data; int PrefixLength; int Appended = 0; Number() { Integer = 0; this->Data.reserve(12); } void Read() { Integer = 0; Data.clear(); std::cin >> Buffer; PrefixLength = Buffer.length(); long long pot = 1; for (int i = 0; i < Buffer.length() - 1; i++) pot *= 10; for (int i = 0; i < Buffer.length(); i++, pot/=10) { Data.push_back(Buffer[i] - '0'); Integer += (long long)Data[i] * pot; } } long long Fill(Number& last) { Appended = 0; long long next = last.Integer+1; if (Integer >= next) return 0; auto nextV = FillNumber(next); int toAdd = 0; bool eq = false; bool append = false; for (int i = 0; i < this->Data.size(); i++) { if (Data[i] < nextV[i]) { append = true; break; } if (Data[i] != nextV[i]) { eq = true; break; } } Appended += last.Appended; if (eq || append) { while (Integer < next) { Integer *= 10; if (Integer >= (long long)1e16) { Appended += append; Integer /= 10; break; } } } else { Integer = next; } FillNumberV(Data, Integer); return Data.size() - PrefixLength + Appended; } void Print() { return; std::cout << "[" << PrefixLength << "]"; for (auto i : Data) { std::cout << (char)(i + '0'); } std::cout << "\n"; } }; void Solve(int n) { long long res = 0; Number last, now; last.Read(); for (int i = 1; i < n; i++) { now.Read(); res += now.Fill(last); last = now; } std::cout << res << "\n"; } int main() { std::ios_base::sync_with_stdio(false); int n; std::cin >> n; Solve(n); 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include <cstdio> #include <algorithm> #include <vector> #include <stack> #include <string> #include <iostream> #include <cmath> std::string Buffer; void FillNumberV(std::vector<char>& v, long long integer) { v.clear(); std::stack<char> s; while (integer) { s.push(integer % 10); integer /= 10; } while (s.size()) v.push_back(s.top()), s.pop(); } std::vector<char> FillNumber(long long integer) { std::vector<char> v; v.reserve(16); FillNumberV(v, integer); return v; } struct Number { long long Integer = 0; std::vector<char> Data; int PrefixLength; int Appended = 0; Number() { Integer = 0; this->Data.reserve(12); } void Read() { Integer = 0; Data.clear(); std::cin >> Buffer; PrefixLength = Buffer.length(); long long pot = 1; for (int i = 0; i < Buffer.length() - 1; i++) pot *= 10; for (int i = 0; i < Buffer.length(); i++, pot/=10) { Data.push_back(Buffer[i] - '0'); Integer += (long long)Data[i] * pot; } } long long Fill(Number& last) { Appended = 0; long long next = last.Integer+1; if (Integer >= next) return 0; auto nextV = FillNumber(next); int toAdd = 0; bool eq = false; bool append = false; for (int i = 0; i < this->Data.size(); i++) { if (Data[i] < nextV[i]) { append = true; break; } if (Data[i] != nextV[i]) { eq = true; break; } } Appended += last.Appended; if (eq || append) { while (Integer < next) { Integer *= 10; if (Integer >= (long long)1e16) { Appended += append; Integer /= 10; break; } } } else { Integer = next; } FillNumberV(Data, Integer); return Data.size() - PrefixLength + Appended; } void Print() { return; std::cout << "[" << PrefixLength << "]"; for (auto i : Data) { std::cout << (char)(i + '0'); } std::cout << "\n"; } }; void Solve(int n) { long long res = 0; Number last, now; last.Read(); for (int i = 1; i < n; i++) { now.Read(); res += now.Fill(last); last = now; } std::cout << res << "\n"; } int main() { std::ios_base::sync_with_stdio(false); int n; std::cin >> n; Solve(n); return 0; } |