#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL> PII; typedef pair<LL, LL> PLL; #define FOR(i, b, e) for (LL i = b; i <= e; i++) #define FORD(i, b, e) for (LL i = b; i >= e; i--) #define SIZE(x) ((LL)(x).size()) #define BOOST ios_base::sync_with_stdio(false); cin.tie(0) #define MP make_pair #define PB push_back #define ST first #define ND second /*************************** END OF TEMPLATE ***************************/ const LL maxn = 200042; LL a[maxn]; LL b[maxn]; LL n; LL zeros[maxn]; LL l[maxn]; string str[maxn]; LL calclength(LL a) { LL r = 0; while(a > 0) { r++; a /= 10; } return r; } bool smaller(LL a, LL b) { while(calclength(a) > calclength(b)) a /= 10; while(calclength(b) > calclength(a)) b /= 10; return a < b; } const LL M = 3000; int main() { BOOST; cin >> n; FOR(i, 1, n) { cin >> a[i]; b[i] = a[i]; } LL ans = 0; str[1] = to_string(a[1]); l[1] = calclength(a[1]); FOR(i, 2, n) { l[i] = calclength(a[i]); if(l[i - 1] > 17) { ans += l[i - 1] - l[i]; l[i] = l[i - 1]; if(smaller(a[i], a[i - 1])) { l[i]++; ans++; } } else { while(calclength(a[i]) < l[i - 1]) { ans++; a[i] *= 10; } l[i] = max(l[i], l[i - 1]); if(a[i] <= a[i - 1]) { bool to_change = false; LL ca = a[i]; LL cb = a[i - 1]; while(ca > 0 && ca < cb) { cb /= 10; ca /= 10; } if(ca < b[i] && a[i] != a[i - 1]) to_change = true; else { a[i] = a[i - 1]; a[i]++; if(calclength(a[i]) > l[i]) to_change = true; else { LL ca = a[i]; LL cb = b[i]; while(calclength(cb) < calclength(ca)) { cb *= 10; } while(ca != cb) { ca /= 10; cb /= 10; } if(ca < b[i]) to_change = true; } } if(to_change) { ans++; l[i]++; a[i] = b[i]; //cout << "????? " << l[i] << endl; while(calclength(a[i]) < l[i]) a[i] *= 10; } } } //cout << b[i] << ":\t" << a[i] << endl; } cout << ans << '\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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL> PII; typedef pair<LL, LL> PLL; #define FOR(i, b, e) for (LL i = b; i <= e; i++) #define FORD(i, b, e) for (LL i = b; i >= e; i--) #define SIZE(x) ((LL)(x).size()) #define BOOST ios_base::sync_with_stdio(false); cin.tie(0) #define MP make_pair #define PB push_back #define ST first #define ND second /*************************** END OF TEMPLATE ***************************/ const LL maxn = 200042; LL a[maxn]; LL b[maxn]; LL n; LL zeros[maxn]; LL l[maxn]; string str[maxn]; LL calclength(LL a) { LL r = 0; while(a > 0) { r++; a /= 10; } return r; } bool smaller(LL a, LL b) { while(calclength(a) > calclength(b)) a /= 10; while(calclength(b) > calclength(a)) b /= 10; return a < b; } const LL M = 3000; int main() { BOOST; cin >> n; FOR(i, 1, n) { cin >> a[i]; b[i] = a[i]; } LL ans = 0; str[1] = to_string(a[1]); l[1] = calclength(a[1]); FOR(i, 2, n) { l[i] = calclength(a[i]); if(l[i - 1] > 17) { ans += l[i - 1] - l[i]; l[i] = l[i - 1]; if(smaller(a[i], a[i - 1])) { l[i]++; ans++; } } else { while(calclength(a[i]) < l[i - 1]) { ans++; a[i] *= 10; } l[i] = max(l[i], l[i - 1]); if(a[i] <= a[i - 1]) { bool to_change = false; LL ca = a[i]; LL cb = a[i - 1]; while(ca > 0 && ca < cb) { cb /= 10; ca /= 10; } if(ca < b[i] && a[i] != a[i - 1]) to_change = true; else { a[i] = a[i - 1]; a[i]++; if(calclength(a[i]) > l[i]) to_change = true; else { LL ca = a[i]; LL cb = b[i]; while(calclength(cb) < calclength(ca)) { cb *= 10; } while(ca != cb) { ca /= 10; cb /= 10; } if(ca < b[i]) to_change = true; } } if(to_change) { ans++; l[i]++; a[i] = b[i]; //cout << "????? " << l[i] << endl; while(calclength(a[i]) < l[i]) a[i] *= 10; } } } //cout << b[i] << ":\t" << a[i] << endl; } cout << ans << '\n'; } |