#include <cstdio> long long int power[] = {1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL, 1000000000000000000LL}; int getDigitsCount(long long int x) { int res = 0; while (x>0) { res++; x/=10; } return res; } long long int getNumberBegin(long long int x, int len) { while (x >= power[len]) x/=10; return x; } int main() { int N; long long int result = 0; long long int begin = 0; long long int extra = 0; scanf("%d",&N); for (int i=0; i<N; ++i) { long long int a; scanf("%lld",&a); if (a>begin) { begin = a; } else { int dc = getDigitsCount(a); long long int x = getNumberBegin(begin, dc); if (a != x || getNumberBegin(begin+1, dc) != x) { while (a <= begin) { a=a*10; result++; } result+=extra; begin = a; if (begin>power[17]) { begin/=10; extra++; } } else { result += getDigitsCount(begin)-dc+extra; begin = begin+1; } } } 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 | #include <cstdio> long long int power[] = {1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL, 1000000000000000000LL}; int getDigitsCount(long long int x) { int res = 0; while (x>0) { res++; x/=10; } return res; } long long int getNumberBegin(long long int x, int len) { while (x >= power[len]) x/=10; return x; } int main() { int N; long long int result = 0; long long int begin = 0; long long int extra = 0; scanf("%d",&N); for (int i=0; i<N; ++i) { long long int a; scanf("%lld",&a); if (a>begin) { begin = a; } else { int dc = getDigitsCount(a); long long int x = getNumberBegin(begin, dc); if (a != x || getNumberBegin(begin+1, dc) != x) { while (a <= begin) { a=a*10; result++; } result+=extra; begin = a; if (begin>power[17]) { begin/=10; extra++; } } else { result += getDigitsCount(begin)-dc+extra; begin = begin+1; } } } printf("%lld\n", result); return 0; } |