#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> #include <string> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) const LL MAX_PREF = 1e16; // very risky to use for other tasks!!! struct BigNumApprox { LL pref; int extra_len; BigNumApprox(int x) : pref(x), extra_len(0) {} bool lt(BigNumApprox &b) { //assert(b.extra_len == 0); return (extra_len == 0) && (pref < b.pref); } bool eq(BigNumApprox &b) { //assert(b.extra_len == 0); return (extra_len == 0) && (pref == b.pref); } void mul10(int k) { //assert(k>=0); if (k == 0) { return; } if (extra_len > 0) { extra_len += k; return; } while(true) { pref *= 10; --k; if (pref >= MAX_PREF) { pref /= 10; extra_len += k+1; return; } if (k==0) { return; } } } int lex_cmp(BigNumApprox &b) { //assert(b.extra_len == 0); auto a_str = to_string(pref); auto b_str = to_string(b.pref); return a_str.compare(b_str); } bool lex_lt(BigNumApprox &b) { //assert(b.extra_len == 0); return lex_cmp(b) < 0; } int lex_len() { return extra_len + to_string(pref).length(); } bool starts_with(BigNumApprox &b) { //assert(b.extra_len == 0); auto a_str = to_string(pref); auto b_str = to_string(b.pref); if (b_str.length() > a_str.length()) { return false; } //https://stackoverflow.com/questions/7913835/check-if-one-string-is-a-prefix-of-another auto res = std::mismatch(b_str.begin(), b_str.end(), a_str.begin()); return (res.first == b_str.end()); } void inc() { ++pref; if (pref == MAX_PREF) { pref /= 10; ++extra_len; } } }; int main() { LL ret=0; int n, aa; scanf("%d\n%d", &n, &aa); BigNumApprox a = BigNumApprox(aa); BigNumApprox b = a; FOR(i,1,n-1) { a = b; int bb; scanf("%d", &bb); b = BigNumApprox(bb); if (a.lt(b)) { continue; } if (a.eq(b)) { b.mul10(1); ret += 1; continue; } //assert(a.gt(b)); int k = a.lex_len() - b.lex_len(); //assert(k>=0); if (a.lex_lt(b)) { b.mul10(k); ret += k; continue; } if (!a.starts_with(b)) { b.mul10(k+1); ret += k+1; continue; } //assert(a.starts_with(b)); a.inc(); if (a.starts_with(b)) { b=a; ret += k; continue; } //a-1 == b99999999 b.mul10(k+1); ret += k+1; continue; } printf("%lld\n", ret); 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 158 159 160 161 162 163 164 165 166 167 168 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> #include <string> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) const LL MAX_PREF = 1e16; // very risky to use for other tasks!!! struct BigNumApprox { LL pref; int extra_len; BigNumApprox(int x) : pref(x), extra_len(0) {} bool lt(BigNumApprox &b) { //assert(b.extra_len == 0); return (extra_len == 0) && (pref < b.pref); } bool eq(BigNumApprox &b) { //assert(b.extra_len == 0); return (extra_len == 0) && (pref == b.pref); } void mul10(int k) { //assert(k>=0); if (k == 0) { return; } if (extra_len > 0) { extra_len += k; return; } while(true) { pref *= 10; --k; if (pref >= MAX_PREF) { pref /= 10; extra_len += k+1; return; } if (k==0) { return; } } } int lex_cmp(BigNumApprox &b) { //assert(b.extra_len == 0); auto a_str = to_string(pref); auto b_str = to_string(b.pref); return a_str.compare(b_str); } bool lex_lt(BigNumApprox &b) { //assert(b.extra_len == 0); return lex_cmp(b) < 0; } int lex_len() { return extra_len + to_string(pref).length(); } bool starts_with(BigNumApprox &b) { //assert(b.extra_len == 0); auto a_str = to_string(pref); auto b_str = to_string(b.pref); if (b_str.length() > a_str.length()) { return false; } //https://stackoverflow.com/questions/7913835/check-if-one-string-is-a-prefix-of-another auto res = std::mismatch(b_str.begin(), b_str.end(), a_str.begin()); return (res.first == b_str.end()); } void inc() { ++pref; if (pref == MAX_PREF) { pref /= 10; ++extra_len; } } }; int main() { LL ret=0; int n, aa; scanf("%d\n%d", &n, &aa); BigNumApprox a = BigNumApprox(aa); BigNumApprox b = a; FOR(i,1,n-1) { a = b; int bb; scanf("%d", &bb); b = BigNumApprox(bb); if (a.lt(b)) { continue; } if (a.eq(b)) { b.mul10(1); ret += 1; continue; } //assert(a.gt(b)); int k = a.lex_len() - b.lex_len(); //assert(k>=0); if (a.lex_lt(b)) { b.mul10(k); ret += k; continue; } if (!a.starts_with(b)) { b.mul10(k+1); ret += k+1; continue; } //assert(a.starts_with(b)); a.inc(); if (a.starts_with(b)) { b=a; ret += k; continue; } //a-1 == b99999999 b.mul10(k+1); ret += k+1; continue; } printf("%lld\n", ret); return 0; } |