#include <cstdio> #include <algorithm> int digit[200005][45]; int nbdigit[200005]; int n; int total; long long res; int main(){ scanf("%d", &n); for (int i = 0; i < n; i++){ int a; scanf("%d", &a); int j = 0; while (a > 0){ nbdigit[i]++; digit[i][j] = a % 10; a /= 10; j++; } for (int k = 0; k <= (j - 1) / 2; k++){ std::swap(digit[i][k], digit[i][j - 1 - k]); } } for (int i = 0; i < n; i++){ if (nbdigit[i] > total){ total = nbdigit[i]; continue; } bool less = false; bool great = false; for (int j = 0; j < nbdigit[i]; j++){ if (digit[i][j] < digit[i - 1][j]){ less = true; break; } if (digit[i][j] > digit[i - 1][j]){ great = true; break; } } if (less){ total++; if (total < 40){ for (int j = nbdigit[i]; j < total; j++){ digit[i][j] = 0; } } } else if (great){ if (total < 40){ for (int j = nbdigit[i]; j < total; j++){ digit[i][j] = 0; } } } else if (total < 40){ int pos = -1; for (int j = total - 1; j >= nbdigit[i]; j--){ if (digit[i - 1][j] < 9){ pos = j; break; } } if (pos == -1){ //printf("a\n"); total++; for (int j = nbdigit[i]; j < total; j++){ digit[i][j] = 0; } } else { //printf("b %d %d\n", pos, digit[i][pos]); for (int j = nbdigit[i]; j < pos; j++){ digit[i][j] = digit[i - 1][j]; } digit[i][pos] = digit[i - 1][pos] + 1; //printf("cc %d\n", digit[i][pos]); for (int j = pos + 1; j < total; j++){ digit[i][j] = 0; } } } res += total - nbdigit[i]; } printf("%lld\n", 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 | #include <cstdio> #include <algorithm> int digit[200005][45]; int nbdigit[200005]; int n; int total; long long res; int main(){ scanf("%d", &n); for (int i = 0; i < n; i++){ int a; scanf("%d", &a); int j = 0; while (a > 0){ nbdigit[i]++; digit[i][j] = a % 10; a /= 10; j++; } for (int k = 0; k <= (j - 1) / 2; k++){ std::swap(digit[i][k], digit[i][j - 1 - k]); } } for (int i = 0; i < n; i++){ if (nbdigit[i] > total){ total = nbdigit[i]; continue; } bool less = false; bool great = false; for (int j = 0; j < nbdigit[i]; j++){ if (digit[i][j] < digit[i - 1][j]){ less = true; break; } if (digit[i][j] > digit[i - 1][j]){ great = true; break; } } if (less){ total++; if (total < 40){ for (int j = nbdigit[i]; j < total; j++){ digit[i][j] = 0; } } } else if (great){ if (total < 40){ for (int j = nbdigit[i]; j < total; j++){ digit[i][j] = 0; } } } else if (total < 40){ int pos = -1; for (int j = total - 1; j >= nbdigit[i]; j--){ if (digit[i - 1][j] < 9){ pos = j; break; } } if (pos == -1){ //printf("a\n"); total++; for (int j = nbdigit[i]; j < total; j++){ digit[i][j] = 0; } } else { //printf("b %d %d\n", pos, digit[i][pos]); for (int j = nbdigit[i]; j < pos; j++){ digit[i][j] = digit[i - 1][j]; } digit[i][pos] = digit[i - 1][pos] + 1; //printf("cc %d\n", digit[i][pos]); for (int j = pos + 1; j < total; j++){ digit[i][j] = 0; } } } res += total - nbdigit[i]; } printf("%lld\n", res); } |