#include<bits/stdc++.h> using namespace std; #define int long long #define PB push_back #define MP make_pair #define FI first #define SE second #define ST FI #define ND SE #define SZ(x) ((int)(x).size()) #define ALL(it, x) for(__typeof(x.begin()) it = x.begin(); it != x.end(); it++) #define REP(i, x) for (int i = 0; i < x; i++) #define FOR(i, x) for (int i = 1; i <= x; i++) #define BACK(i, x) for (int i = x; i; i--) typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; template<typename TH> void _dbg(const char* s, TH h) { cerr<<s<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* s, TH h, TA... t) { while(*s != ',') {cerr<<*s++;} cerr<<"="<<h<<","; _dbg(s+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; ALL(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif const int MN = 200 * 1000 + 50; //!!!!!!!!!!!!!!!! //const int MN = 50; //!!!!!!!!!!!!!!!! int n, res = 0, ndig = 0, nzpref = 1, nzsuf = 0; vector<int> dig; int take_digit(int b, int nb, int i) { return (int)(b / pow(10, nb - 1 - i)) % 10; } void set_zeros(int b, int e) { for(int i = max(0ll, b); i < e && i < ndig; i++) { dig[i] = 0; } } void set_zeros_from(int nb) { set_zeros(nb, nzpref); nzpref = nb; set_zeros(ndig - nzsuf, ndig); nzsuf = 0; } void extend() { dig[ndig] = 0; ndig++; } void update_nzsuf(int i) { if (i + 1 + nzsuf == ndig && i >= nzpref) { nzsuf++; } } bool maybe_add_one(int nb) { if (nb >= ndig) return false; dig[ndig - 1]++; update_nzsuf(ndig - 1); for (int i = ndig - 1; i >= nb + 1; i--) { if (dig[i] >= 10) { dig[i] -= 10; dig[i - 1] += 1; update_nzsuf(i - 1); } else { break; } } if (dig[nb] >= 10) { dig[nb] = 0; return false; } return true; } int process(int b) { int nb = log10(b) + 1; debug(nb); if (nb > ndig) { ndig = nb; nzpref = nb; nzsuf = 0; for (int i = 0; i < nb; i++) { dig[i] = take_digit(b, nb, i); } return 0; } int d; int sgn = 0; REP(i, nb) { d = take_digit(b, nb, i); if (sgn == 0) { if (d > dig[i]) sgn = 1; if (d < dig[i]) sgn = -1; } dig[i] = d; nzpref = max(nzpref, i+1); } debug(sgn); if (sgn == 1) { set_zeros_from(nb); return ndig - nb; } else if (sgn == 0) { //add one and carry one, and maybe extend if (maybe_add_one(nb)) { return ndig - nb; } else { set_zeros_from(nb); extend(); return ndig - nb; } } else { set_zeros_from(nb); extend(); return ndig - nb; } return 0; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(7) << fixed; int b; cin >> n; dig.resize(MN); REP(i, n) { cin >> b; int r = process(b); debug(b); //debugv(dig); debug(r, ndig, nzpref, nzsuf); res += r; } 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 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 | #include<bits/stdc++.h> using namespace std; #define int long long #define PB push_back #define MP make_pair #define FI first #define SE second #define ST FI #define ND SE #define SZ(x) ((int)(x).size()) #define ALL(it, x) for(__typeof(x.begin()) it = x.begin(); it != x.end(); it++) #define REP(i, x) for (int i = 0; i < x; i++) #define FOR(i, x) for (int i = 1; i <= x; i++) #define BACK(i, x) for (int i = x; i; i--) typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; template<typename TH> void _dbg(const char* s, TH h) { cerr<<s<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* s, TH h, TA... t) { while(*s != ',') {cerr<<*s++;} cerr<<"="<<h<<","; _dbg(s+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; ALL(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif const int MN = 200 * 1000 + 50; //!!!!!!!!!!!!!!!! //const int MN = 50; //!!!!!!!!!!!!!!!! int n, res = 0, ndig = 0, nzpref = 1, nzsuf = 0; vector<int> dig; int take_digit(int b, int nb, int i) { return (int)(b / pow(10, nb - 1 - i)) % 10; } void set_zeros(int b, int e) { for(int i = max(0ll, b); i < e && i < ndig; i++) { dig[i] = 0; } } void set_zeros_from(int nb) { set_zeros(nb, nzpref); nzpref = nb; set_zeros(ndig - nzsuf, ndig); nzsuf = 0; } void extend() { dig[ndig] = 0; ndig++; } void update_nzsuf(int i) { if (i + 1 + nzsuf == ndig && i >= nzpref) { nzsuf++; } } bool maybe_add_one(int nb) { if (nb >= ndig) return false; dig[ndig - 1]++; update_nzsuf(ndig - 1); for (int i = ndig - 1; i >= nb + 1; i--) { if (dig[i] >= 10) { dig[i] -= 10; dig[i - 1] += 1; update_nzsuf(i - 1); } else { break; } } if (dig[nb] >= 10) { dig[nb] = 0; return false; } return true; } int process(int b) { int nb = log10(b) + 1; debug(nb); if (nb > ndig) { ndig = nb; nzpref = nb; nzsuf = 0; for (int i = 0; i < nb; i++) { dig[i] = take_digit(b, nb, i); } return 0; } int d; int sgn = 0; REP(i, nb) { d = take_digit(b, nb, i); if (sgn == 0) { if (d > dig[i]) sgn = 1; if (d < dig[i]) sgn = -1; } dig[i] = d; nzpref = max(nzpref, i+1); } debug(sgn); if (sgn == 1) { set_zeros_from(nb); return ndig - nb; } else if (sgn == 0) { //add one and carry one, and maybe extend if (maybe_add_one(nb)) { return ndig - nb; } else { set_zeros_from(nb); extend(); return ndig - nb; } } else { set_zeros_from(nb); extend(); return ndig - nb; } return 0; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(7) << fixed; int b; cin >> n; dig.resize(MN); REP(i, n) { cin >> b; int r = process(b); debug(b); //debugv(dig); debug(r, ndig, nzpref, nzsuf); res += r; } cout << res << endl; return 0; } |