#include <iostream> typedef long long ll; using namespace std; ll digits(ll a) { if (a <= 0) { return 1; } int digs = 0; while (a > 0) { digs++; a/=10; } return digs; } class LongNumber { public: ll prefix; ll suffix; ll trailing_length; ll total_length; LongNumber(ll a) : prefix(a), suffix(0), trailing_length(0), total_length(digits(a)) { } LongNumber(ll a, ll length) : prefix(a), suffix(0), trailing_length(length - digits(a)), total_length(length) { } void increment() { if (trailing_length == 0) { prefix++; total_length = digits(prefix); return; } suffix++; if (digits(suffix) == trailing_length) { for (int i=0;i<trailing_length;i++) { prefix*=10; } prefix += suffix; trailing_length = 0; suffix = 0; } } ll firstN(ll n) { if (this->total_length < n) { return -1; } ll prefix_length = digits(this->prefix); ll suffix_length = digits(this->suffix); ll suffix_digits_to_take = n - (total_length - suffix_length); ll result = this->prefix; if (prefix_length >= n) { while (prefix_length > n) { result/=10; prefix_length--; } return result; } while (prefix_length < n) { result*=10; prefix_length++; } ll suffix_digits = this->suffix; if (suffix_digits_to_take > 0) { while(suffix_length > suffix_digits_to_take) { suffix_length--; suffix_digits/=10; } result += suffix_digits; } return result; } void print() { ll suffix_length = digits(suffix); cerr<<prefix; if (trailing_length - suffix_length > 0) { cerr << " <-- " << trailing_length - suffix_length << " 0s -->"; } if (trailing_length && suffix_length) { cerr << suffix; } cerr << endl; } }; int main() { ll N; ll additions = 0; LongNumber current(0); scanf("%lld", &N); while(N--) { ll a; scanf("%lld", &a); ll digs = digits(a); ll first_n = current.firstN(digs); if (first_n == -1) { current = LongNumber(a); } else if (a < first_n) { additions += current.total_length - digs + 1; current = LongNumber(a, current.total_length+1); } else if (a > first_n) { additions += current.total_length - digs; current = LongNumber(a, current.total_length); } else /* a == first_n */ { int total_digs = current.total_length; current.increment(); first_n = current.firstN(digs); if (a == first_n && total_digs == current.total_length) { additions += current.total_length - digs; } else { additions += total_digs - digs + 1; current = LongNumber(a, total_digs+1); } } //current.print(); } cout << additions << 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 128 | #include <iostream> typedef long long ll; using namespace std; ll digits(ll a) { if (a <= 0) { return 1; } int digs = 0; while (a > 0) { digs++; a/=10; } return digs; } class LongNumber { public: ll prefix; ll suffix; ll trailing_length; ll total_length; LongNumber(ll a) : prefix(a), suffix(0), trailing_length(0), total_length(digits(a)) { } LongNumber(ll a, ll length) : prefix(a), suffix(0), trailing_length(length - digits(a)), total_length(length) { } void increment() { if (trailing_length == 0) { prefix++; total_length = digits(prefix); return; } suffix++; if (digits(suffix) == trailing_length) { for (int i=0;i<trailing_length;i++) { prefix*=10; } prefix += suffix; trailing_length = 0; suffix = 0; } } ll firstN(ll n) { if (this->total_length < n) { return -1; } ll prefix_length = digits(this->prefix); ll suffix_length = digits(this->suffix); ll suffix_digits_to_take = n - (total_length - suffix_length); ll result = this->prefix; if (prefix_length >= n) { while (prefix_length > n) { result/=10; prefix_length--; } return result; } while (prefix_length < n) { result*=10; prefix_length++; } ll suffix_digits = this->suffix; if (suffix_digits_to_take > 0) { while(suffix_length > suffix_digits_to_take) { suffix_length--; suffix_digits/=10; } result += suffix_digits; } return result; } void print() { ll suffix_length = digits(suffix); cerr<<prefix; if (trailing_length - suffix_length > 0) { cerr << " <-- " << trailing_length - suffix_length << " 0s -->"; } if (trailing_length && suffix_length) { cerr << suffix; } cerr << endl; } }; int main() { ll N; ll additions = 0; LongNumber current(0); scanf("%lld", &N); while(N--) { ll a; scanf("%lld", &a); ll digs = digits(a); ll first_n = current.firstN(digs); if (first_n == -1) { current = LongNumber(a); } else if (a < first_n) { additions += current.total_length - digs + 1; current = LongNumber(a, current.total_length+1); } else if (a > first_n) { additions += current.total_length - digs; current = LongNumber(a, current.total_length); } else /* a == first_n */ { int total_digs = current.total_length; current.increment(); first_n = current.firstN(digs); if (a == first_n && total_digs == current.total_length) { additions += current.total_length - digs; } else { additions += total_digs - digs + 1; current = LongNumber(a, total_digs+1); } } //current.print(); } cout << additions << endl; return 0; } |