#include <iostream> #define uint unsigned int #define LL long long using namespace std; const short threshold = 15; uint n; LL previous, current; short digitsCount[2]; LL zeros[2]; LL pow[19]; //tablica z kolejnymi potegami 10 void GetDigits(short tab, LL num) //wpisywanie kolejnych cyfr liczby do danej tablicy (w kolejnosci odwroconej!) { short index = 0; while(num > 0) { num /= 10; index++; } digitsCount[tab] = index; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); pow[0] = 1; for(uint i = 1; i < 19; i++) pow[i] = pow[i-1] * 10; LL result = 0; cin >> n; cin >> previous; for(uint i = 1; i < n; i++) { cin >> current; if(current > previous && zeros[0] == 0) { previous = current; digitsCount[0] = 0; continue; } if(digitsCount[0] == 0) GetDigits(0, previous); GetDigits(1, current); LL rest = 0; short delta = digitsCount[0] - digitsCount[1]; //roznica w liczbie cyfr miedzy obecna liczba a poprzednia LL zerosDelta = zeros[0]; current *= pow[delta]; digitsCount[1] += delta; zeros[1] = zeros[0]; result += zerosDelta + delta; if(delta > 0) { rest = previous % pow[delta]; if(previous - rest == current) { if((rest + 1) % pow[delta] == 0) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } else current += rest + 1; } else if(current < previous) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } } else { if(current < previous) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } else if(current == previous && zeros[1] == 0) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } } previous = current; digitsCount[0] = digitsCount[1]; zeros[0] = zeros[1]; } cout << 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 126 127 128 129 130 131 132 133 134 135 136 137 | #include <iostream> #define uint unsigned int #define LL long long using namespace std; const short threshold = 15; uint n; LL previous, current; short digitsCount[2]; LL zeros[2]; LL pow[19]; //tablica z kolejnymi potegami 10 void GetDigits(short tab, LL num) //wpisywanie kolejnych cyfr liczby do danej tablicy (w kolejnosci odwroconej!) { short index = 0; while(num > 0) { num /= 10; index++; } digitsCount[tab] = index; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); pow[0] = 1; for(uint i = 1; i < 19; i++) pow[i] = pow[i-1] * 10; LL result = 0; cin >> n; cin >> previous; for(uint i = 1; i < n; i++) { cin >> current; if(current > previous && zeros[0] == 0) { previous = current; digitsCount[0] = 0; continue; } if(digitsCount[0] == 0) GetDigits(0, previous); GetDigits(1, current); LL rest = 0; short delta = digitsCount[0] - digitsCount[1]; //roznica w liczbie cyfr miedzy obecna liczba a poprzednia LL zerosDelta = zeros[0]; current *= pow[delta]; digitsCount[1] += delta; zeros[1] = zeros[0]; result += zerosDelta + delta; if(delta > 0) { rest = previous % pow[delta]; if(previous - rest == current) { if((rest + 1) % pow[delta] == 0) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } else current += rest + 1; } else if(current < previous) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } } else { if(current < previous) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } else if(current == previous && zeros[1] == 0) { if(digitsCount[1] == threshold) { zeros[1]++; } else { current *= 10; digitsCount[1]++; } result++; } } previous = current; digitsCount[0] = digitsCount[1]; zeros[0] = zeros[1]; } cout << result; return 0; } |