#include <bits/stdc++.h> #define mp make_pair #define fi first #define se second #define pb push_back #define all(x) (x).begin(),(x).end() using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector < int > VI; typedef double D; const int MN = 100005, inf = 1000000005, mod = 1000000007, L = 25; const LL INF = 1000000000000000005LL; char liczba[L]; char C[L]; int curLen = 0, l; LL res = 0LL; int daj(int a, int b) { for(int i = b - 1; i >= a; --i) if(liczba[i] != '9') return i; return -1; } void doloz() { int dif = (curLen + 1 - l); res += dif; curLen += 1; for(int i = 0; i < l; ++i) liczba[i] = C[i]; for(int i = l; i < min(L, curLen); ++i) liczba[i] = '0'; } int main() { int n; scanf("%d", &n); while(n--) { scanf("%s", C); l = strlen(C); if(l > curLen) { curLen = l; for(int i = 0; i < l; ++i) liczba[i] = C[i]; continue; } int zla = 0, dobra = 0; for(int i = 0; i < l; ++i) if(liczba[i] < C[i]) { dobra = 1; break; } else if(liczba[i] > C[i]) { zla = 1; break; } if(zla || (!dobra && l == curLen)) { doloz(); } else if(dobra) { int dif = (curLen - l); res += dif; for(int i = 0; i < l; ++i) liczba[i] = C[i]; for(int i = l; i < min(L, curLen); ++i) liczba[i] = '0'; } else { int pos = daj(l, min(curLen, L)); if(pos != -1) { res += (curLen - l); for(int i = pos + 1; i < min(curLen, L); ++i) liczba[i] = '0'; liczba[pos]++; } else { doloz(); } } } printf("%lld", res); }
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 | #include <bits/stdc++.h> #define mp make_pair #define fi first #define se second #define pb push_back #define all(x) (x).begin(),(x).end() using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector < int > VI; typedef double D; const int MN = 100005, inf = 1000000005, mod = 1000000007, L = 25; const LL INF = 1000000000000000005LL; char liczba[L]; char C[L]; int curLen = 0, l; LL res = 0LL; int daj(int a, int b) { for(int i = b - 1; i >= a; --i) if(liczba[i] != '9') return i; return -1; } void doloz() { int dif = (curLen + 1 - l); res += dif; curLen += 1; for(int i = 0; i < l; ++i) liczba[i] = C[i]; for(int i = l; i < min(L, curLen); ++i) liczba[i] = '0'; } int main() { int n; scanf("%d", &n); while(n--) { scanf("%s", C); l = strlen(C); if(l > curLen) { curLen = l; for(int i = 0; i < l; ++i) liczba[i] = C[i]; continue; } int zla = 0, dobra = 0; for(int i = 0; i < l; ++i) if(liczba[i] < C[i]) { dobra = 1; break; } else if(liczba[i] > C[i]) { zla = 1; break; } if(zla || (!dobra && l == curLen)) { doloz(); } else if(dobra) { int dif = (curLen - l); res += dif; for(int i = 0; i < l; ++i) liczba[i] = C[i]; for(int i = l; i < min(L, curLen); ++i) liczba[i] = '0'; } else { int pos = daj(l, min(curLen, L)); if(pos != -1) { res += (curLen - l); for(int i = pos + 1; i < min(curLen, L); ++i) liczba[i] = '0'; liczba[pos]++; } else { doloz(); } } } printf("%lld", res); } |