#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef pair <ii,int> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; struct num { vector<int> l; vector<int> r; int numZ; int getLen() { return l.size() + r.size() + numZ; } int get(int indexT) { if (indexT < l.size()) return l[indexT]; indexT -= l.size(); if (indexT < numZ) return 0; indexT -= numZ; return r[indexT]; } int getLastNotNine() { int indexT = -1, len = getLen(), i; for (i = len - 1;i>=0;i--) if (get(i) != 9) { indexT = i; break; } return indexT; } void inc() { int indexT = getLastNotNine(), i; if (indexT == -1 || indexT < l.size()) { numZ = numZ + r.size() + 1; r.clear(); return; } if (indexT < l.size() + numZ) { numZ--; int s = r.size(); r.clear(); r.pb(1); while (s--) r.pb(0); return; } indexT -= (l.size() + numZ); r[indexT]++; for (i = indexT + 1;i<r.size();i++) r[i] = 0; } void print() { for (int i=0;i<getLen();i++) printf("%d", get(i)); printf("\n"); } }; num con(int x) { num res; res.l.clear(); res.r.clear(); res.numZ = 0; while (x > 0) { res.l.pb(x % 10); x /= 10; } reverse(res.l.begin(), res.l.end()); return res; } int n, a, primLen, c, i, indexT; LL res; num r, last; VI wek; int main() { scanf("%d", &n); scanf("%d", &a); last = con(a); res = 0; n--; while (n--) { scanf("%d", &a); r = con(a); primLen = r.getLen(); if (r.getLen() > last.getLen()) { last = r; continue; } for (i=0;i < r.l.size();i++) if (r.l[i] != last.get(i)) { if (r.l[i] > last.get(i)) { r.numZ += (last.getLen() - r.getLen()); } else { r.numZ += (last.getLen() - r.getLen() + 1); } last = r; break; } if (i == r.l.size()) { indexT = last.getLastNotNine(); if (indexT != -1 && indexT < last.l.size() && indexT >= r.l.size()) { wek.clear(); for (i=r.l.size();i<last.l.size();i++) wek.pb(last.l[i]); for (i=r.l.size();i<last.l.size();i++) last.l.pop_back(); for (i=0;i<last.r.size();i++) wek.pb(last.r[i]); last.r = wek; } last.inc(); } //last.print(); res += last.getLen() - primLen; } 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 115 116 117 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef pair <ii,int> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; struct num { vector<int> l; vector<int> r; int numZ; int getLen() { return l.size() + r.size() + numZ; } int get(int indexT) { if (indexT < l.size()) return l[indexT]; indexT -= l.size(); if (indexT < numZ) return 0; indexT -= numZ; return r[indexT]; } int getLastNotNine() { int indexT = -1, len = getLen(), i; for (i = len - 1;i>=0;i--) if (get(i) != 9) { indexT = i; break; } return indexT; } void inc() { int indexT = getLastNotNine(), i; if (indexT == -1 || indexT < l.size()) { numZ = numZ + r.size() + 1; r.clear(); return; } if (indexT < l.size() + numZ) { numZ--; int s = r.size(); r.clear(); r.pb(1); while (s--) r.pb(0); return; } indexT -= (l.size() + numZ); r[indexT]++; for (i = indexT + 1;i<r.size();i++) r[i] = 0; } void print() { for (int i=0;i<getLen();i++) printf("%d", get(i)); printf("\n"); } }; num con(int x) { num res; res.l.clear(); res.r.clear(); res.numZ = 0; while (x > 0) { res.l.pb(x % 10); x /= 10; } reverse(res.l.begin(), res.l.end()); return res; } int n, a, primLen, c, i, indexT; LL res; num r, last; VI wek; int main() { scanf("%d", &n); scanf("%d", &a); last = con(a); res = 0; n--; while (n--) { scanf("%d", &a); r = con(a); primLen = r.getLen(); if (r.getLen() > last.getLen()) { last = r; continue; } for (i=0;i < r.l.size();i++) if (r.l[i] != last.get(i)) { if (r.l[i] > last.get(i)) { r.numZ += (last.getLen() - r.getLen()); } else { r.numZ += (last.getLen() - r.getLen() + 1); } last = r; break; } if (i == r.l.size()) { indexT = last.getLastNotNine(); if (indexT != -1 && indexT < last.l.size() && indexT >= r.l.size()) { wek.clear(); for (i=r.l.size();i<last.l.size();i++) wek.pb(last.l[i]); for (i=r.l.size();i<last.l.size();i++) last.l.pop_back(); for (i=0;i<last.r.size();i++) wek.pb(last.r[i]); last.r = wek; } last.inc(); } //last.print(); res += last.getLen() - primLen; } cout << res << endl; return 0; } |