#include <bits/stdc++.h> #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; const int max_n = 3e5 + 5; const int INF = 1e9 + 7; int n; VI v; map<int,int> nines; lint get_val(int nr, int zer, int suf) { lint ans = nr; while (zer--) { ans = ans * 10LL; if (ans > INF) return INF; } if (suf == -1) return ans; int nsuf = suf; while (nsuf > 0) { ans *= 10LL; nsuf /= 10; if (ans > INF) return INF; } ans += suf; if (ans > INF) return INF; return ans; } pair<PII,int> inc(int nr, int zer, int suf) { if (suf == -1) { if (zer == 0) return {{nr + 1,0},-1}; return {{nr,zer-1},1}; } if (nines.find(suf) == nines.end()) { return {{nr,zer},suf+1}; } if (zer == 0) { return {{nr+1,nines[suf]},-1}; } return {{nr,zer-1},suf+1}; } int num_len(int k) { if (k == 0) return 1; int ans = 0; while (k) { ans++; k /= 10; } return ans; } int len(int nr, int zer, int suf) { int ans = zer; ans += num_len(nr); if (suf == -1) return ans; ans += num_len(suf); return ans; } int is_pref(int nr, int zer, int suf, int kto) { int l = num_len(kto); VI digs; int cnr = nr; while (cnr) { digs.pb(cnr%10); cnr /= 10; } reverse(all(digs)); while (zer && digs.size() < l) { digs.pb(0); zer--; } VI sdigs; while (suf) { sdigs.pb(suf%10); suf /= 10; } reverse(all(sdigs)); FOR(i,0,sdigs.size()) digs.pb(sdigs[i]); if (digs.size() < l) return -1000; VI kdigs; while (kto) { kdigs.pb(kto%10); kto /= 10; } reverse(all(kdigs)); FOR(i,0,l) { if (kdigs[i] < digs[i]) return -1; if (kdigs[i] > digs[i]) return 1; } return 0; } int main() { lint act = 0; int cnt = 0; while (act < INF) { cnt++; act = 10LL * act + 9; nines[act] = cnt; } read(n); readv(v, n); lint ans = 0; int active = v[0]; int zer = 0; int suf = -1; FOR(i,1,n) { // debug(ans); // debug3(active, zer, suf); lint w = get_val(active, zer, suf); // debug(w); if (w < v[i]) { active = v[i]; zer = 0; suf = -1; continue; } else { pair<PII, int> dupa = inc(active, zer, suf); int is_p = is_pref(dupa.st.st, dupa.st.nd, dupa.nd, v[i]); // debug(is_p); if (is_p == 0) { active = dupa.st.st; zer = dupa.st.nd; suf = dupa.nd; ans += len(active, zer, suf) - num_len(v[i]); } else if (is_p == -1) { int lv = num_len(v[i]); int l = len(dupa.st.st, dupa.st.nd, dupa.nd); ans += l-lv+1; active = v[i]; zer = l-lv+1; suf = -1; } else if (is_p == 1) { int lv = num_len(v[i]); int l = len(dupa.st.st, dupa.st.nd , dupa.nd); ans += l-lv; active = v[i]; zer = l-lv; suf = -1; } } } // debug3(active, zer, suf); printf("%lld\n", ans); }
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #include <bits/stdc++.h> #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; const int max_n = 3e5 + 5; const int INF = 1e9 + 7; int n; VI v; map<int,int> nines; lint get_val(int nr, int zer, int suf) { lint ans = nr; while (zer--) { ans = ans * 10LL; if (ans > INF) return INF; } if (suf == -1) return ans; int nsuf = suf; while (nsuf > 0) { ans *= 10LL; nsuf /= 10; if (ans > INF) return INF; } ans += suf; if (ans > INF) return INF; return ans; } pair<PII,int> inc(int nr, int zer, int suf) { if (suf == -1) { if (zer == 0) return {{nr + 1,0},-1}; return {{nr,zer-1},1}; } if (nines.find(suf) == nines.end()) { return {{nr,zer},suf+1}; } if (zer == 0) { return {{nr+1,nines[suf]},-1}; } return {{nr,zer-1},suf+1}; } int num_len(int k) { if (k == 0) return 1; int ans = 0; while (k) { ans++; k /= 10; } return ans; } int len(int nr, int zer, int suf) { int ans = zer; ans += num_len(nr); if (suf == -1) return ans; ans += num_len(suf); return ans; } int is_pref(int nr, int zer, int suf, int kto) { int l = num_len(kto); VI digs; int cnr = nr; while (cnr) { digs.pb(cnr%10); cnr /= 10; } reverse(all(digs)); while (zer && digs.size() < l) { digs.pb(0); zer--; } VI sdigs; while (suf) { sdigs.pb(suf%10); suf /= 10; } reverse(all(sdigs)); FOR(i,0,sdigs.size()) digs.pb(sdigs[i]); if (digs.size() < l) return -1000; VI kdigs; while (kto) { kdigs.pb(kto%10); kto /= 10; } reverse(all(kdigs)); FOR(i,0,l) { if (kdigs[i] < digs[i]) return -1; if (kdigs[i] > digs[i]) return 1; } return 0; } int main() { lint act = 0; int cnt = 0; while (act < INF) { cnt++; act = 10LL * act + 9; nines[act] = cnt; } read(n); readv(v, n); lint ans = 0; int active = v[0]; int zer = 0; int suf = -1; FOR(i,1,n) { // debug(ans); // debug3(active, zer, suf); lint w = get_val(active, zer, suf); // debug(w); if (w < v[i]) { active = v[i]; zer = 0; suf = -1; continue; } else { pair<PII, int> dupa = inc(active, zer, suf); int is_p = is_pref(dupa.st.st, dupa.st.nd, dupa.nd, v[i]); // debug(is_p); if (is_p == 0) { active = dupa.st.st; zer = dupa.st.nd; suf = dupa.nd; ans += len(active, zer, suf) - num_len(v[i]); } else if (is_p == -1) { int lv = num_len(v[i]); int l = len(dupa.st.st, dupa.st.nd, dupa.nd); ans += l-lv+1; active = v[i]; zer = l-lv+1; suf = -1; } else if (is_p == 1) { int lv = num_len(v[i]); int l = len(dupa.st.st, dupa.st.nd , dupa.nd); ans += l-lv; active = v[i]; zer = l-lv; suf = -1; } } } // debug3(active, zer, suf); printf("%lld\n", ans); } |