#include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct result { int digits; unsigned long number; }; const int MAX_NUMBERS = 200001; const int MAX_DIGITS = 11; int n; unsigned long needed = 0; unsigned long a[MAX_NUMBERS]; unsigned int actualDigits[MAX_DIGITS]; unsigned int lowerDigits[MAX_DIGITS]; inline bool endsWithAllNines(int lod) { bool answer = true; for (int i = 0; i <= lod; i++) { answer = answer && (lowerDigits[i] == 9); } return answer; } result fillNumber(unsigned long actual, unsigned long lower) { result res; res.digits = 0; res.number = actual; int actualDigitsCount = 0; int lowerDigitsCount = 0; unsigned long number; if (actual > lower) { return res; } else if (actual == lower) { res.digits = 1; res.number = actual * 10; return res; } number = actual; while (number != 0) { actualDigits[actualDigitsCount] = number % 10; number /= 10; actualDigitsCount++; } number = lower; while (number != 0) { lowerDigits[lowerDigitsCount] = number % 10; number /= 10; lowerDigitsCount++; } int acd = actualDigitsCount - 1; int lod = lowerDigitsCount - 1; while (acd >= 0 && lod >= 0 && actualDigits[acd] == lowerDigits[lod]) { acd--; lod--; } if (acd >= 0 && lod >= 0) { if (actualDigits[acd] > lowerDigits[lod]) { res.digits = lowerDigitsCount - actualDigitsCount; for (int i = 0; i < res.digits; i++) { res.number *= 10; } } else { res.digits = lowerDigitsCount - actualDigitsCount + 1; for (int i = 0; i < res.digits; i++) { res.number *= 10; } } } else { if (endsWithAllNines(lod)) { res.digits = lod + 2; for (int i = 0; i < res.digits; i++) { res.number *= 10; } } else { res.digits = lod + 1; res.number = lower + 1; } } return res; } int main() { scanf("%d\n", &n); for (int i = 0; i < n; i++) { scanf("%lu\n", &a[i]); } for (int i = 1; i < n; i++) { if (a[i] <= a[i-1]) { result res = fillNumber(a[i], a[i-1]); needed += res.digits; a[i] = res.number; } } printf("%lu\n", needed); 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 | #include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct result { int digits; unsigned long number; }; const int MAX_NUMBERS = 200001; const int MAX_DIGITS = 11; int n; unsigned long needed = 0; unsigned long a[MAX_NUMBERS]; unsigned int actualDigits[MAX_DIGITS]; unsigned int lowerDigits[MAX_DIGITS]; inline bool endsWithAllNines(int lod) { bool answer = true; for (int i = 0; i <= lod; i++) { answer = answer && (lowerDigits[i] == 9); } return answer; } result fillNumber(unsigned long actual, unsigned long lower) { result res; res.digits = 0; res.number = actual; int actualDigitsCount = 0; int lowerDigitsCount = 0; unsigned long number; if (actual > lower) { return res; } else if (actual == lower) { res.digits = 1; res.number = actual * 10; return res; } number = actual; while (number != 0) { actualDigits[actualDigitsCount] = number % 10; number /= 10; actualDigitsCount++; } number = lower; while (number != 0) { lowerDigits[lowerDigitsCount] = number % 10; number /= 10; lowerDigitsCount++; } int acd = actualDigitsCount - 1; int lod = lowerDigitsCount - 1; while (acd >= 0 && lod >= 0 && actualDigits[acd] == lowerDigits[lod]) { acd--; lod--; } if (acd >= 0 && lod >= 0) { if (actualDigits[acd] > lowerDigits[lod]) { res.digits = lowerDigitsCount - actualDigitsCount; for (int i = 0; i < res.digits; i++) { res.number *= 10; } } else { res.digits = lowerDigitsCount - actualDigitsCount + 1; for (int i = 0; i < res.digits; i++) { res.number *= 10; } } } else { if (endsWithAllNines(lod)) { res.digits = lod + 2; for (int i = 0; i < res.digits; i++) { res.number *= 10; } } else { res.digits = lod + 1; res.number = lower + 1; } } return res; } int main() { scanf("%d\n", &n); for (int i = 0; i < n; i++) { scanf("%lu\n", &a[i]); } for (int i = 1; i < n; i++) { if (a[i] <= a[i-1]) { result res = fillNumber(a[i], a[i-1]); needed += res.digits; a[i] = res.number; } } printf("%lu\n", needed); return 0; } |