#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <string> #include <set> #include <unordered_set> #include <unordered_map> #include <cmath> #include <cstdio> #include <algorithm> #include <stack> #include <iomanip> #include <limits> #include <cctype> using namespace std; void fill(int x, vector<int>& v) { int i = 0; while(x > 0 && i < v.size()) { v[i++] = x % 10; x /= 10; } while(x > 0) { v.push_back(x % 10); x /= 10; ++i; } v.resize(i); } void add_one(vector<int>& v) { int i = 0; ++v[i]; while(i + 1 < v.size() && v[i] == 10) { ++v[i + 1]; v[i] = 0; ++i; } if(v[i] > 9) { v.push_back(1); v[i] = 0; } } int pad_with_zero(vector<int>& v, const vector<int>& u, int n) { v.resize(n + u.size()); for(int i = 0; i < n; ++i) { v[i] = 0; } for(int i = 0; i < u.size(); ++i) { v[n + i] = u[i]; } return n; } int check_prefix(const vector<int>& u, const vector<int>& v) { for(int i = u.size() - 1, j = v.size() - 1; i >= 0 && j >= 0; --i, --j) { if(u[i] > v[j]) { return 1; } else if(u[i] < v[j]) { return -1; } } return 0; } int main(int argc, char const *argv[]) { std::ios::sync_with_stdio(false); int n, x, p, v_size; long long res = 0; cin >> n >> x; vector<int> v, u; fill(x, v); for(int i = 1; i < n; ++i) { cin >> x; fill(x, u); if(u.size() > v.size()) { v = u; } else if(u.size() == v.size()) { p = check_prefix(u, v); if(p == 0) { res += pad_with_zero(v, u, 1); } else if(p == 1) { v = u; } else { res += pad_with_zero(v, u, 1); } } else { p = check_prefix(u, v); if(p == 0) { v_size = v.size(); add_one(v); if(check_prefix(u, v) == 0) { res += v_size - u.size(); } else { res += pad_with_zero(v, u, v_size - u.size() + 1); } } else if(p == 1) { res += pad_with_zero(v, u, v.size() - u.size()); } else { res += pad_with_zero(v, u, v.size() - u.size() + 1); } } } cout << res << 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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <string> #include <set> #include <unordered_set> #include <unordered_map> #include <cmath> #include <cstdio> #include <algorithm> #include <stack> #include <iomanip> #include <limits> #include <cctype> using namespace std; void fill(int x, vector<int>& v) { int i = 0; while(x > 0 && i < v.size()) { v[i++] = x % 10; x /= 10; } while(x > 0) { v.push_back(x % 10); x /= 10; ++i; } v.resize(i); } void add_one(vector<int>& v) { int i = 0; ++v[i]; while(i + 1 < v.size() && v[i] == 10) { ++v[i + 1]; v[i] = 0; ++i; } if(v[i] > 9) { v.push_back(1); v[i] = 0; } } int pad_with_zero(vector<int>& v, const vector<int>& u, int n) { v.resize(n + u.size()); for(int i = 0; i < n; ++i) { v[i] = 0; } for(int i = 0; i < u.size(); ++i) { v[n + i] = u[i]; } return n; } int check_prefix(const vector<int>& u, const vector<int>& v) { for(int i = u.size() - 1, j = v.size() - 1; i >= 0 && j >= 0; --i, --j) { if(u[i] > v[j]) { return 1; } else if(u[i] < v[j]) { return -1; } } return 0; } int main(int argc, char const *argv[]) { std::ios::sync_with_stdio(false); int n, x, p, v_size; long long res = 0; cin >> n >> x; vector<int> v, u; fill(x, v); for(int i = 1; i < n; ++i) { cin >> x; fill(x, u); if(u.size() > v.size()) { v = u; } else if(u.size() == v.size()) { p = check_prefix(u, v); if(p == 0) { res += pad_with_zero(v, u, 1); } else if(p == 1) { v = u; } else { res += pad_with_zero(v, u, 1); } } else { p = check_prefix(u, v); if(p == 0) { v_size = v.size(); add_one(v); if(check_prefix(u, v) == 0) { res += v_size - u.size(); } else { res += pad_with_zero(v, u, v_size - u.size() + 1); } } else if(p == 1) { res += pad_with_zero(v, u, v.size() - u.size()); } else { res += pad_with_zero(v, u, v.size() - u.size() + 1); } } } cout << res << endl; return 0; } |