#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; #define NUM_SIZE 16 int tab[NUM_SIZE]; inline void roz(int x){ FOR(i,0,NUM_SIZE){ tab[i] = x % 10; x /= 10; } } int len(int x){ int ans = 0; while(x > 0){ ans++; x /= 10; } return ans; } void prtnum(int* num, int beg, int add_l, int ans){ FOR(j, beg, NUM_SIZE) cout << num[j]; cout << " -> beg=" << NUM_SIZE - beg << ", add_len=" << add_l << ", ans=" << ans << endl; } int comparePref(int * num, int lenp, int beg){ int cmppref = 0; FORD(i,lenp-1,0){ if (tab[i] < num[beg - i + lenp - 1]){ cmppref = -1; break; } else if (tab[i] > num[beg - i + lenp - 1]){ cmppref = 1; break; } } return cmppref; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int num[NUM_SIZE]; FOR(i,0,NUM_SIZE) num[i] = 0; ll ans = 0; int beg = NUM_SIZE; int add_len = 0, p, lenp; FOR(liczba,0,n){ cin >> p; lenp = len(p); if (lenp > NUM_SIZE - beg){ beg = NUM_SIZE; while(p > 0){ beg--; num[beg] = p % 10; p /= 10; } } else { roz(p); int cmppref = comparePref(num, lenp, beg); if(cmppref < 0){ if (beg > 0){ beg--; } else { add_len++; } ans += add_len + NUM_SIZE - beg - lenp; FORD(i,lenp-1,0) num[beg - i + lenp - 1] = tab[i]; FOR(i,beg + lenp,NUM_SIZE) num[i] = 0; } else if (cmppref == 0){ if (beg > 0){ int nnc = 0; FOR(i,beg + lenp,NUM_SIZE){ if (num[i] != 9) nnc++; } if (nnc > 0){ num[NUM_SIZE - 1]++; FORD(i,NUM_SIZE-1,1){ num[i - 1] += num[i] / 10; num[i] %= 10; } } else { beg--; FORD(i,lenp-1,0) num[beg - i + lenp - 1] = tab[i]; FOR(i,beg + lenp,NUM_SIZE) num[i] = 0; } } ans += add_len + NUM_SIZE - beg - lenp; } else { FORD(i,lenp-1,0) num[beg - i + lenp - 1] = tab[i]; FOR(i,beg + lenp,NUM_SIZE) num[i] = 0; ans += add_len + NUM_SIZE - beg - lenp; } } //prtnum(num, beg, add_len, ans); } cout << ans << endl; }
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 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; #define NUM_SIZE 16 int tab[NUM_SIZE]; inline void roz(int x){ FOR(i,0,NUM_SIZE){ tab[i] = x % 10; x /= 10; } } int len(int x){ int ans = 0; while(x > 0){ ans++; x /= 10; } return ans; } void prtnum(int* num, int beg, int add_l, int ans){ FOR(j, beg, NUM_SIZE) cout << num[j]; cout << " -> beg=" << NUM_SIZE - beg << ", add_len=" << add_l << ", ans=" << ans << endl; } int comparePref(int * num, int lenp, int beg){ int cmppref = 0; FORD(i,lenp-1,0){ if (tab[i] < num[beg - i + lenp - 1]){ cmppref = -1; break; } else if (tab[i] > num[beg - i + lenp - 1]){ cmppref = 1; break; } } return cmppref; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int num[NUM_SIZE]; FOR(i,0,NUM_SIZE) num[i] = 0; ll ans = 0; int beg = NUM_SIZE; int add_len = 0, p, lenp; FOR(liczba,0,n){ cin >> p; lenp = len(p); if (lenp > NUM_SIZE - beg){ beg = NUM_SIZE; while(p > 0){ beg--; num[beg] = p % 10; p /= 10; } } else { roz(p); int cmppref = comparePref(num, lenp, beg); if(cmppref < 0){ if (beg > 0){ beg--; } else { add_len++; } ans += add_len + NUM_SIZE - beg - lenp; FORD(i,lenp-1,0) num[beg - i + lenp - 1] = tab[i]; FOR(i,beg + lenp,NUM_SIZE) num[i] = 0; } else if (cmppref == 0){ if (beg > 0){ int nnc = 0; FOR(i,beg + lenp,NUM_SIZE){ if (num[i] != 9) nnc++; } if (nnc > 0){ num[NUM_SIZE - 1]++; FORD(i,NUM_SIZE-1,1){ num[i - 1] += num[i] / 10; num[i] %= 10; } } else { beg--; FORD(i,lenp-1,0) num[beg - i + lenp - 1] = tab[i]; FOR(i,beg + lenp,NUM_SIZE) num[i] = 0; } } ans += add_len + NUM_SIZE - beg - lenp; } else { FORD(i,lenp-1,0) num[beg - i + lenp - 1] = tab[i]; FOR(i,beg + lenp,NUM_SIZE) num[i] = 0; ans += add_len + NUM_SIZE - beg - lenp; } } //prtnum(num, beg, add_len, ans); } cout << ans << endl; } |