#include <iostream> #include <algorithm> using namespace std; long long n, ans, power[15] = {1}; long long calcLvl(long long x) { long long res = 0; do { res++; } while (x /= 10); return res; } int main() { ios_base::sync_with_stdio(0); for (int i = 1; i < 15; i++) power[i] = 10 * power[i - 1]; cin >> n; for (long long i = 1, x, lastX = 0ll, xNew = 0ll, lastCalcX = 0ll, zerosCount = 0ll, lvlShift = 0ll, maxLvl = 0ll, currLvl = 0ll; i <= n; i++) { cin >> x; currLvl = calcLvl(x); maxLvl = max(maxLvl, currLvl); if (maxLvl < 15) { if (power[max(0ll, maxLvl - currLvl)] * x < lastCalcX) { zerosCount = 0; if (calcLvl(lastCalcX) >= currLvl) { if (x >= lastCalcX / power[calcLvl(lastCalcX) - currLvl] && x >= (lastCalcX + 1) / power[calcLvl(lastCalcX) - currLvl]) { xNew = lastCalcX + 1; } else { maxLvl++; xNew = power[maxLvl - currLvl] * x; } } else { maxLvl++; xNew = power[maxLvl - currLvl] * x; } } else if (power[max(0ll, maxLvl - currLvl)] * x == lastCalcX) { zerosCount++; if (maxLvl == currLvl || zerosCount == power[maxLvl - currLvl]) { zerosCount = 0; maxLvl++; } xNew = power[maxLvl - currLvl] * x + zerosCount; } else { zerosCount = 0; xNew = power[maxLvl - currLvl] * x; } lastX = x; } else { currLvl = calcLvl(x); long long lastLvl = calcLvl(lastX); if (currLvl < lastLvl) { if (x < lastX / power[lastLvl - currLvl]) { maxLvl++; lastX = x; } if (x > lastX / power[lastLvl - currLvl]) { lastX = x; } } else if (currLvl == lastLvl) { if (x < lastX) { maxLvl++; } lastX = x; } else { if (x < lastX * power[currLvl - lastLvl]) { maxLvl++; } lastX = x; } } ans += maxLvl - currLvl; lastCalcX = xNew; } cout << ans; }
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 | #include <iostream> #include <algorithm> using namespace std; long long n, ans, power[15] = {1}; long long calcLvl(long long x) { long long res = 0; do { res++; } while (x /= 10); return res; } int main() { ios_base::sync_with_stdio(0); for (int i = 1; i < 15; i++) power[i] = 10 * power[i - 1]; cin >> n; for (long long i = 1, x, lastX = 0ll, xNew = 0ll, lastCalcX = 0ll, zerosCount = 0ll, lvlShift = 0ll, maxLvl = 0ll, currLvl = 0ll; i <= n; i++) { cin >> x; currLvl = calcLvl(x); maxLvl = max(maxLvl, currLvl); if (maxLvl < 15) { if (power[max(0ll, maxLvl - currLvl)] * x < lastCalcX) { zerosCount = 0; if (calcLvl(lastCalcX) >= currLvl) { if (x >= lastCalcX / power[calcLvl(lastCalcX) - currLvl] && x >= (lastCalcX + 1) / power[calcLvl(lastCalcX) - currLvl]) { xNew = lastCalcX + 1; } else { maxLvl++; xNew = power[maxLvl - currLvl] * x; } } else { maxLvl++; xNew = power[maxLvl - currLvl] * x; } } else if (power[max(0ll, maxLvl - currLvl)] * x == lastCalcX) { zerosCount++; if (maxLvl == currLvl || zerosCount == power[maxLvl - currLvl]) { zerosCount = 0; maxLvl++; } xNew = power[maxLvl - currLvl] * x + zerosCount; } else { zerosCount = 0; xNew = power[maxLvl - currLvl] * x; } lastX = x; } else { currLvl = calcLvl(x); long long lastLvl = calcLvl(lastX); if (currLvl < lastLvl) { if (x < lastX / power[lastLvl - currLvl]) { maxLvl++; lastX = x; } if (x > lastX / power[lastLvl - currLvl]) { lastX = x; } } else if (currLvl == lastLvl) { if (x < lastX) { maxLvl++; } lastX = x; } else { if (x < lastX * power[currLvl - lastLvl]) { maxLvl++; } lastX = x; } } ans += maxLvl - currLvl; lastCalcX = xNew; } cout << ans; } |