#include<bits/stdc++.h> using namespace std; typedef long long int lld; lld count(lld v){ if(v == 0) return 1; lld res = 0; while(v > 0) res++, v /= 10; return res; } lld MX[] = {0, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999}; lld A2[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}; lld getMX(int n){ return MX[(n<8 ? n : 8)]; } struct Number{ lld v; lld cnt; lld add; lld len; Number(void): cnt(0), add(-1) {} lld getVal(void){ lld k = count(v); lld l = k+cnt; lld res = v; while(k++ <= min(k+cnt,15ll)) res *= 10; return res+max(0ll,add); } int getLength(void){ return cnt+len; } void increase(void){ add++; if(add == 0 && cnt > 0) add++; if(cnt < count(add)) cnt++, add = -1; } }; void split(vector<lld>& res, lld a){ res.clear(); while(a > 0) res.push_back(a), a /= 10; reverse(res.begin(), res.end()); } bool operator < (Number& a, Number& b){ if(a.getLength() != b.getLength()) return a.getLength() < b.getLength(); return a.getVal() < b.getVal(); } vector<lld> x,y; bool isPrefix(const Number& a, const Number& b){ split(x, a.v); split(y, b.v); if(x.size() >= y.size()) return false; for(int i=0;i<x.size();i++) if(x[i] != y[i]) return false; return true; } int main(void){ ios_base::sync_with_stdio(false); int n; cin >> n; vector<Number> t(n); for(auto& i : t){ cin >> i.v; i.len = count(i.v); } lld res = 0; for(int i=1;i<n;i++){ t[i].cnt = t[i-1].getLength() - t[i].len; if(t[i].cnt < 0) t[i].cnt = 0; if(t[i].getLength() == t[i-1].getLength() && (t[i].v == t[i-1].v || isPrefix(t[i], t[i-1]))){ res += max(0ll, count(t[i-1].v) - count(t[i].v)); if(t[i-1].cnt == 0 && count(t[i].v) != count(t[i-1].v)){ t[i] = t[i-1]; t[i].v++; res += count(t[i].v) - count(t[i-1].v); }else{ t[i] = t[i-1]; t[i].increase(); } } while(t[i].getLength() < t[i-1].getLength()) t[i].cnt++, t[i].add=-1; lld o = t[i].add; t[i].add = getMX(t[i].cnt); if(!(t[i-1] < t[i])){ t[i].cnt++, t[i].add=-1; continue; } t[i].add = 0; for(lld j=min(t[i].cnt,5ll);j>=1;j--) for(lld k=1;k<=9;k++){ t[i].add += A2[j]; if(!(t[i] < t[i-1])){ t[i].add -= A2[j]; continue; } } while(!(t[i-1] < t[i])) t[i].increase(); } for(auto& i : t) res += i.cnt; /* for(auto& i : t){ cout << i.v; for(int j=0;j<i.cnt-count(i.add);j++) cout << "0"; if(i.add >= 0) cout << i.add; cout << "\n"; } */ cout << res << "\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 149 150 151 152 153 154 155 156 157 | #include<bits/stdc++.h> using namespace std; typedef long long int lld; lld count(lld v){ if(v == 0) return 1; lld res = 0; while(v > 0) res++, v /= 10; return res; } lld MX[] = {0, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999}; lld A2[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}; lld getMX(int n){ return MX[(n<8 ? n : 8)]; } struct Number{ lld v; lld cnt; lld add; lld len; Number(void): cnt(0), add(-1) {} lld getVal(void){ lld k = count(v); lld l = k+cnt; lld res = v; while(k++ <= min(k+cnt,15ll)) res *= 10; return res+max(0ll,add); } int getLength(void){ return cnt+len; } void increase(void){ add++; if(add == 0 && cnt > 0) add++; if(cnt < count(add)) cnt++, add = -1; } }; void split(vector<lld>& res, lld a){ res.clear(); while(a > 0) res.push_back(a), a /= 10; reverse(res.begin(), res.end()); } bool operator < (Number& a, Number& b){ if(a.getLength() != b.getLength()) return a.getLength() < b.getLength(); return a.getVal() < b.getVal(); } vector<lld> x,y; bool isPrefix(const Number& a, const Number& b){ split(x, a.v); split(y, b.v); if(x.size() >= y.size()) return false; for(int i=0;i<x.size();i++) if(x[i] != y[i]) return false; return true; } int main(void){ ios_base::sync_with_stdio(false); int n; cin >> n; vector<Number> t(n); for(auto& i : t){ cin >> i.v; i.len = count(i.v); } lld res = 0; for(int i=1;i<n;i++){ t[i].cnt = t[i-1].getLength() - t[i].len; if(t[i].cnt < 0) t[i].cnt = 0; if(t[i].getLength() == t[i-1].getLength() && (t[i].v == t[i-1].v || isPrefix(t[i], t[i-1]))){ res += max(0ll, count(t[i-1].v) - count(t[i].v)); if(t[i-1].cnt == 0 && count(t[i].v) != count(t[i-1].v)){ t[i] = t[i-1]; t[i].v++; res += count(t[i].v) - count(t[i-1].v); }else{ t[i] = t[i-1]; t[i].increase(); } } while(t[i].getLength() < t[i-1].getLength()) t[i].cnt++, t[i].add=-1; lld o = t[i].add; t[i].add = getMX(t[i].cnt); if(!(t[i-1] < t[i])){ t[i].cnt++, t[i].add=-1; continue; } t[i].add = 0; for(lld j=min(t[i].cnt,5ll);j>=1;j--) for(lld k=1;k<=9;k++){ t[i].add += A2[j]; if(!(t[i] < t[i-1])){ t[i].add -= A2[j]; continue; } } while(!(t[i-1] < t[i])) t[i].increase(); } for(auto& i : t) res += i.cnt; /* for(auto& i : t){ cout << i.v; for(int j=0;j<i.cnt-count(i.add);j++) cout << "0"; if(i.add >= 0) cout << i.add; cout << "\n"; } */ cout << res << "\n"; return 0; } |