#include<bits/stdc++.h> #define ull unsigned long long #define pb push_back #define MP make_pair using namespace std; const ull M = 2e5 + 2137; const ull graniczna = 1e15; ull a[M]; ull ile_cyfr[M]; int policz(ull n){ int cnt = 0; while(n > 0){ n /= 10; cnt++; } return cnt; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int N; cin >> N; for(int i = 0; i < N; i++){ cin >> a[i]; int tmp = a[i], cnt = 1; while((tmp /= 10) > 0) cnt++; ile_cyfr[i] = cnt; } ull wyn = 0; for(int i = 1; i < N; i++){ //cout << "Patrze na " << a[i] << endl; if(a[i - 1] >= graniczna){ int roznica = policz(a[i-1]) - ile_cyfr[i]; int r = roznica; int prawr = ile_cyfr[i - 1] - ile_cyfr[i]; ull tmp = a[i - 1], ogon = 0, pot = 1; while(roznica--){ ogon += (tmp % 10) * pot; pot *= 10; tmp /= 10; } //cout << "Teraz porownuje " << tmp << " z " << a[i] << endl; if(tmp < a[i]){ wyn += prawr; ile_cyfr[i] += prawr; while(r--) a[i] *= 10; } else if(tmp > a[i]){ prawr++; wyn += prawr; ile_cyfr[i] += prawr; while(r--) a[i] *= 10; } else{ wyn += prawr; ile_cyfr[i] += prawr; a[i] = a[i - 1]; //while(r--) a[i] *= 10; } } else if(a[i - 1] >= a[i]){ int roznica = ile_cyfr[i - 1] - ile_cyfr[i]; int r = roznica; ull tmp = a[i - 1], ogon = 0, pot = 1; while(roznica--){ ogon += (tmp % 10) * pot; pot *= 10; tmp /= 10; } //cout << "Porownuje " << tmp << " + " << ogon << " z liczba " << a[i] << endl; if(tmp < a[i]){ wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; } else if(tmp > a[i]){ r++; wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; } else{ ull pom9 = 0; int pomr = r; while(pomr--){ pom9 *= 10; pom9 += 9; } if(r > 0 && ogon < pom9){ wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; a[i] += ogon + 1; } else{ r++; wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; } } } //cout << "a[" << i << "] wynosi " << a[i] << endl; } cout << wyn; 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 | #include<bits/stdc++.h> #define ull unsigned long long #define pb push_back #define MP make_pair using namespace std; const ull M = 2e5 + 2137; const ull graniczna = 1e15; ull a[M]; ull ile_cyfr[M]; int policz(ull n){ int cnt = 0; while(n > 0){ n /= 10; cnt++; } return cnt; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int N; cin >> N; for(int i = 0; i < N; i++){ cin >> a[i]; int tmp = a[i], cnt = 1; while((tmp /= 10) > 0) cnt++; ile_cyfr[i] = cnt; } ull wyn = 0; for(int i = 1; i < N; i++){ //cout << "Patrze na " << a[i] << endl; if(a[i - 1] >= graniczna){ int roznica = policz(a[i-1]) - ile_cyfr[i]; int r = roznica; int prawr = ile_cyfr[i - 1] - ile_cyfr[i]; ull tmp = a[i - 1], ogon = 0, pot = 1; while(roznica--){ ogon += (tmp % 10) * pot; pot *= 10; tmp /= 10; } //cout << "Teraz porownuje " << tmp << " z " << a[i] << endl; if(tmp < a[i]){ wyn += prawr; ile_cyfr[i] += prawr; while(r--) a[i] *= 10; } else if(tmp > a[i]){ prawr++; wyn += prawr; ile_cyfr[i] += prawr; while(r--) a[i] *= 10; } else{ wyn += prawr; ile_cyfr[i] += prawr; a[i] = a[i - 1]; //while(r--) a[i] *= 10; } } else if(a[i - 1] >= a[i]){ int roznica = ile_cyfr[i - 1] - ile_cyfr[i]; int r = roznica; ull tmp = a[i - 1], ogon = 0, pot = 1; while(roznica--){ ogon += (tmp % 10) * pot; pot *= 10; tmp /= 10; } //cout << "Porownuje " << tmp << " + " << ogon << " z liczba " << a[i] << endl; if(tmp < a[i]){ wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; } else if(tmp > a[i]){ r++; wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; } else{ ull pom9 = 0; int pomr = r; while(pomr--){ pom9 *= 10; pom9 += 9; } if(r > 0 && ogon < pom9){ wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; a[i] += ogon + 1; } else{ r++; wyn += r; ile_cyfr[i] += r; while(r--) a[i] *= 10; } } } //cout << "a[" << i << "] wynosi " << a[i] << endl; } cout << wyn; return 0; } |