#include <cstdio> #define MAXN 200001 #define LIMIT 1000000000000000 long long int n, X[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000,1000000000000000 }; long long int res; typedef struct complex { long long int base = 0, zeros = 0; } Complex; Complex C[MAXN]; int next(int a, int b) { if (C[a].base < C[b].base) { C[b].zeros = C[a].zeros; return C[b].zeros; } long long int i = 0, temp = C[b].base; for (; temp <= C[a].base; temp *= 10, i++); if (temp / 10 == C[a].base && C[a].zeros) { C[b].base = C[a].base; C[b].zeros = C[a].zeros; return C[b].zeros + i - 1; } else if (C[a].base - temp / 10 < X[i - 1] - 1) { if (C[a].zeros) { C[b].base = C[a].base; C[b].zeros = C[a].zeros; return i - 1 + C[b].zeros; } else { C[b].base = C[a].base + 1; return i - 1; } } else { if (temp >= LIMIT) { C[b].base = temp / 10; C[b].zeros = C[a].zeros + 1; return i - 1 + C[b].zeros; } else { C[b].base = temp; C[b].zeros = C[a].zeros; return i + C[b].zeros; } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &C[i].base); for (int i = 1; i < n; i++) { int x = next(i - 1, i); //printf("%lld + %lld %d\n", C[i].base, C[i].zeros, x); res += x; } printf("%lld\n", res); 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 | #include <cstdio> #define MAXN 200001 #define LIMIT 1000000000000000 long long int n, X[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000,1000000000000000 }; long long int res; typedef struct complex { long long int base = 0, zeros = 0; } Complex; Complex C[MAXN]; int next(int a, int b) { if (C[a].base < C[b].base) { C[b].zeros = C[a].zeros; return C[b].zeros; } long long int i = 0, temp = C[b].base; for (; temp <= C[a].base; temp *= 10, i++); if (temp / 10 == C[a].base && C[a].zeros) { C[b].base = C[a].base; C[b].zeros = C[a].zeros; return C[b].zeros + i - 1; } else if (C[a].base - temp / 10 < X[i - 1] - 1) { if (C[a].zeros) { C[b].base = C[a].base; C[b].zeros = C[a].zeros; return i - 1 + C[b].zeros; } else { C[b].base = C[a].base + 1; return i - 1; } } else { if (temp >= LIMIT) { C[b].base = temp / 10; C[b].zeros = C[a].zeros + 1; return i - 1 + C[b].zeros; } else { C[b].base = temp; C[b].zeros = C[a].zeros; return i + C[b].zeros; } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &C[i].base); for (int i = 1; i < n; i++) { int x = next(i - 1, i); //printf("%lld + %lld %d\n", C[i].base, C[i].zeros, x); res += x; } printf("%lld\n", res); return 0; } |