#pragma region Template #include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < (n); i++) #define ForD(i, n) for (int i = (n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #pragma endregion bool strict_less(const string &lhs, const string &rhs) { if (lhs.size() != rhs.size()) return lhs.size() < rhs.size(); return lhs < rhs; } typedef pair<string, ll> number; ll get_size(const number &num) { return (ll)num.first.size() + num.second; } bool operator< (number lhs, number rhs) { if (get_size(lhs) != get_size(rhs)) return get_size(lhs) < get_size(rhs); while (lhs.first.size() < rhs.first.size()) lhs.first.push_back('0'); while (lhs.first.size() > rhs.first.size()) rhs.first.push_back('0'); return strict_less(lhs.first, rhs.first); } pair<ll, number> get_larger_than(const number &than, number curr) { if (than < curr) return { 0, curr }; int than_len = (int)than.first.size(); int curr_len = (int)curr.first.size(); auto small_than = than; deque<char> last_digits; For (i, curr_len - than_len) { small_than.first.push_back('0'); small_than.second--; } For (i, than_len - curr_len) { last_digits.push_front(small_than.first.back()); small_than.first.pop_back(); } if (strict_less(small_than.first, curr.first)) { ll add0s = (ll)last_digits.size() + small_than.second - curr.second; curr.second += add0s; return { add0s, curr }; } else if (strict_less(curr.first, small_than.first)) { ll add0s = (ll)last_digits.size() + small_than.second - curr.second + 1; curr.second += add0s; return { add0s, curr }; } else { if (small_than.second + small_than.first.size() < 18) { while (small_than.second > 0) { last_digits.push_back('0'); small_than.second--; } int non9_pos = -1; ForD (i, (int)last_digits.size()) { char d = last_digits[i]; if (d != '9') { non9_pos = i; break; } } int res = 0; For (i, (int)last_digits.size()) { char d = last_digits[i]; res++; if (i == non9_pos) curr.first.push_back(char(d + 1)); else if (i < non9_pos) curr.first.push_back(d); else { curr.second++; } } if (non9_pos == -1) { curr.second++; res++; } error(res, last_digits.size()); return { res, curr }; } else { auto s_curr = get_size(curr); auto s_than = get_size(than); return { s_than - s_curr, than }; } } } int main() { _upgrade; int n; cin >> n; number prev = {"0", 0LL}; ll res = 0; For (i, n) { string x; cin >> x; auto p = get_larger_than(prev, {x, 0LL}); res += p.first; prev = p.second; } cout << res << "\n"; }
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 | #pragma region Template #include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < (n); i++) #define ForD(i, n) for (int i = (n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #pragma endregion bool strict_less(const string &lhs, const string &rhs) { if (lhs.size() != rhs.size()) return lhs.size() < rhs.size(); return lhs < rhs; } typedef pair<string, ll> number; ll get_size(const number &num) { return (ll)num.first.size() + num.second; } bool operator< (number lhs, number rhs) { if (get_size(lhs) != get_size(rhs)) return get_size(lhs) < get_size(rhs); while (lhs.first.size() < rhs.first.size()) lhs.first.push_back('0'); while (lhs.first.size() > rhs.first.size()) rhs.first.push_back('0'); return strict_less(lhs.first, rhs.first); } pair<ll, number> get_larger_than(const number &than, number curr) { if (than < curr) return { 0, curr }; int than_len = (int)than.first.size(); int curr_len = (int)curr.first.size(); auto small_than = than; deque<char> last_digits; For (i, curr_len - than_len) { small_than.first.push_back('0'); small_than.second--; } For (i, than_len - curr_len) { last_digits.push_front(small_than.first.back()); small_than.first.pop_back(); } if (strict_less(small_than.first, curr.first)) { ll add0s = (ll)last_digits.size() + small_than.second - curr.second; curr.second += add0s; return { add0s, curr }; } else if (strict_less(curr.first, small_than.first)) { ll add0s = (ll)last_digits.size() + small_than.second - curr.second + 1; curr.second += add0s; return { add0s, curr }; } else { if (small_than.second + small_than.first.size() < 18) { while (small_than.second > 0) { last_digits.push_back('0'); small_than.second--; } int non9_pos = -1; ForD (i, (int)last_digits.size()) { char d = last_digits[i]; if (d != '9') { non9_pos = i; break; } } int res = 0; For (i, (int)last_digits.size()) { char d = last_digits[i]; res++; if (i == non9_pos) curr.first.push_back(char(d + 1)); else if (i < non9_pos) curr.first.push_back(d); else { curr.second++; } } if (non9_pos == -1) { curr.second++; res++; } error(res, last_digits.size()); return { res, curr }; } else { auto s_curr = get_size(curr); auto s_than = get_size(than); return { s_than - s_curr, than }; } } } int main() { _upgrade; int n; cin >> n; number prev = {"0", 0LL}; ll res = 0; For (i, n) { string x; cin >> x; auto p = get_larger_than(prev, {x, 0LL}); res += p.first; prev = p.second; } cout << res << "\n"; } |