#include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long ll; int digits(int n) { int res = 0; while (n > 0) { n /= 10; ++res; } return res; } struct state { vector<char> v; int n; state(int x) { while (x > 0) { v.push_back(x % 10); x /= 10; } reverse(v.begin(), v.end()); n = v.size(); } state(int x, int total) { while (x > 0) { v.push_back(x % 10); x /= 10; } reverse(v.begin(), v.end()); while (v.size() < total && v.size() < 17) v.push_back(0); n = total; } int size() { return n; } int cmp(state s) { for (int i=0; i<s.size(); ++i) if (v[i] != s.v[i]) return v[i] < s.v[i] ? -1 : 1; return 0; } bool canInc(int start) { for (int i=start; i<v.size(); ++i) if (v[i] != 9) return true; return false; } void inc(int start) { ++v.back(); for (int i=v.size()-1; i>start; --i) { if (v[i] < 10) break; v[i] -= 10; v[i-1]++; } } void show() { return; for (int i=0; i<v.size(); ++i) printf("%d", v[i]); printf(" (%d)", n); printf("\n"); } }; ll rres; state next(state a, int bb) { state b(bb); if (a.size() < b.size()) return b; int ccc = a.cmp(b); if (a.size() == b.size()) { if (ccc < 0) return b; state res(bb, b.size() + 1); rres += res.size() - b.size(); return res; } if (ccc < 0) { state res(bb, a.size()); rres += res.size() - b.size(); return res; } if (ccc > 0) { state res(bb, a.size() + 1); rres += res.size() - b.size(); return res; } if (a.canInc(b.size())) { a.inc(b.size()); rres += a.size() - b.size(); return a; } state res(bb, a.size() + 1); rres += res.size() - b.size(); return res; } int read() { int n; scanf("%d", &n); return n; } ll solve() { rres = 0; int n = read(); state s(read()); s.show(); for (int i=1; i<n; ++i) { s = next(s, read()); s.show(); } return rres; } int main() { ll res = solve(); printf("%lld\n", res); 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long ll; int digits(int n) { int res = 0; while (n > 0) { n /= 10; ++res; } return res; } struct state { vector<char> v; int n; state(int x) { while (x > 0) { v.push_back(x % 10); x /= 10; } reverse(v.begin(), v.end()); n = v.size(); } state(int x, int total) { while (x > 0) { v.push_back(x % 10); x /= 10; } reverse(v.begin(), v.end()); while (v.size() < total && v.size() < 17) v.push_back(0); n = total; } int size() { return n; } int cmp(state s) { for (int i=0; i<s.size(); ++i) if (v[i] != s.v[i]) return v[i] < s.v[i] ? -1 : 1; return 0; } bool canInc(int start) { for (int i=start; i<v.size(); ++i) if (v[i] != 9) return true; return false; } void inc(int start) { ++v.back(); for (int i=v.size()-1; i>start; --i) { if (v[i] < 10) break; v[i] -= 10; v[i-1]++; } } void show() { return; for (int i=0; i<v.size(); ++i) printf("%d", v[i]); printf(" (%d)", n); printf("\n"); } }; ll rres; state next(state a, int bb) { state b(bb); if (a.size() < b.size()) return b; int ccc = a.cmp(b); if (a.size() == b.size()) { if (ccc < 0) return b; state res(bb, b.size() + 1); rres += res.size() - b.size(); return res; } if (ccc < 0) { state res(bb, a.size()); rres += res.size() - b.size(); return res; } if (ccc > 0) { state res(bb, a.size() + 1); rres += res.size() - b.size(); return res; } if (a.canInc(b.size())) { a.inc(b.size()); rres += a.size() - b.size(); return a; } state res(bb, a.size() + 1); rres += res.size() - b.size(); return res; } int read() { int n; scanf("%d", &n); return n; } ll solve() { rres = 0; int n = read(); state s(read()); s.show(); for (int i=1; i<n; ++i) { s = next(s, read()); s.show(); } return rres; } int main() { ll res = solve(); printf("%lld\n", res); return 0; } |