#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, a[N]; string b[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = to_string(a[i]); //printf("%s\n", b[i].c_str()); } string act = b[1]; int dod = 0; long long result = 0; for (int i = 2; i <= n; i++) { while ((int) act.size() > 20) { dod++; act.pop_back(); } //printf("%s %d\n>%s\n", act.c_str(), dod, b[i].c_str()); int poz = -1; for (int j = 0; j < (int) min(act.size(), b[i].size()); j++) if (b[i][j] != act[j]) { poz = j; break; } if (dod == 0) { if ((int) b[i].size() > (int) act.size()) { act = b[i]; continue; } if (b[i][poz] > act[poz]) { while ((int) b[i].size() < (int) act.size()) { b[i] += '0'; result++; } act = b[i]; continue; } if (b[i][poz] < act[poz]) { while ((int) b[i].size() <= (int) act.size()) { b[i] += '0'; result++; } act = b[i]; continue; } bool nie_dziewiec = false; for (int j = (int) b[i].size(); j < (int) act.size(); j++) if (act[j] != '9') nie_dziewiec = true; if (nie_dziewiec) { result += -(int)b[i].size()+(int)act.size(); act[(int) act.size() - 1]++; for (int j = (int) act.size() - 1; j >= 0; j--) if (act[j] > '9') { act[j] -= 10; act[j-1]++; } continue; } else { while ((int) b[i].size() <= (int) act.size()) { b[i] += '0'; result++; } act = b[i]; continue; } } else { int len = (int) act.size() + dod; if (poz != -1 && b[i][poz] < act[poz]) { len++; result += len - (int) b[i].size(); while ((int) b[i].size() < (int) act.size()) b[i] += '0'; act = b[i]; dod = len - (int) b[i].size(); } if (poz != -1 && b[i][poz] > act[poz]) { result += len - (int) b[i].size(); while ((int) b[i].size() < (int) act.size()) b[i] += '0'; act = b[i]; dod = len - (int) b[i].size(); } if (poz == -1) { result += len - (int) b[i].size(); for (int j = (int) b[i].size(); j < (int) act.size(); j++) b[i] += act[j]; act = b[i]; dod = len - (int) b[i].size(); } } } printf("%lld\n", result); 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 | #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, a[N]; string b[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = to_string(a[i]); //printf("%s\n", b[i].c_str()); } string act = b[1]; int dod = 0; long long result = 0; for (int i = 2; i <= n; i++) { while ((int) act.size() > 20) { dod++; act.pop_back(); } //printf("%s %d\n>%s\n", act.c_str(), dod, b[i].c_str()); int poz = -1; for (int j = 0; j < (int) min(act.size(), b[i].size()); j++) if (b[i][j] != act[j]) { poz = j; break; } if (dod == 0) { if ((int) b[i].size() > (int) act.size()) { act = b[i]; continue; } if (b[i][poz] > act[poz]) { while ((int) b[i].size() < (int) act.size()) { b[i] += '0'; result++; } act = b[i]; continue; } if (b[i][poz] < act[poz]) { while ((int) b[i].size() <= (int) act.size()) { b[i] += '0'; result++; } act = b[i]; continue; } bool nie_dziewiec = false; for (int j = (int) b[i].size(); j < (int) act.size(); j++) if (act[j] != '9') nie_dziewiec = true; if (nie_dziewiec) { result += -(int)b[i].size()+(int)act.size(); act[(int) act.size() - 1]++; for (int j = (int) act.size() - 1; j >= 0; j--) if (act[j] > '9') { act[j] -= 10; act[j-1]++; } continue; } else { while ((int) b[i].size() <= (int) act.size()) { b[i] += '0'; result++; } act = b[i]; continue; } } else { int len = (int) act.size() + dod; if (poz != -1 && b[i][poz] < act[poz]) { len++; result += len - (int) b[i].size(); while ((int) b[i].size() < (int) act.size()) b[i] += '0'; act = b[i]; dod = len - (int) b[i].size(); } if (poz != -1 && b[i][poz] > act[poz]) { result += len - (int) b[i].size(); while ((int) b[i].size() < (int) act.size()) b[i] += '0'; act = b[i]; dod = len - (int) b[i].size(); } if (poz == -1) { result += len - (int) b[i].size(); for (int j = (int) b[i].size(); j < (int) act.size(); j++) b[i] += act[j]; act = b[i]; dod = len - (int) b[i].size(); } } } printf("%lld\n", result); return 0; } |