// Author: Krzysztof Bochenek // Email: kpbochenek@gmail.com // -------------------------------- #include <stdio.h> #include <vector> #include <map> #include <set> #include <stack> #include <math.h> #include <algorithm> #include <string> #include <iostream> #include <climits> #define pb push_back #define mp make_pair using namespace std; typedef long long int ll; typedef unsigned long long ull; #define rep(i, n) for(int i=0; i<n; ++i) int a[200001]; string slast, scurrent; string lastBigStr; ull lastBigStrSize; int do_compare(/*slast, scurrent*/) { for (int i=0; i<scurrent.size(); ++i) { if (slast[i] < scurrent[i]) { return -1; } else if (slast[i] > scurrent[i]) { return 1; } } return 0; } int do_compare_big(/*lastBigStr, scurrent*/) { for (int i=0; i<scurrent.size(); ++i) { if (lastBigStr[i] < scurrent[i]) { return -1; } else if (lastBigStr[i] > scurrent[i]) { return 1; } } return 0; } void cpx() { for (int i=0; i<20; ++i) { if (i < scurrent.size()) { lastBigStr[i] = scurrent[i]; } else { lastBigStr[i] = '0'; } } } int main() { ios::sync_with_stdio(0); ull result = 0; int N; cin >> N; for (int i=0; i<N; ++i) { cin >> a[i]; } ull last = 0; for (int i=0; i<N; ++i) { ull current = a[i]; if (last < 1000000000000000000) { if (last >= current) { slast = to_string(last); scurrent = to_string(current); if (slast.size() == scurrent.size()) { current *= 10; result++; } else { int compared = do_compare(/*slast, scurrent*/); if (compared == 0) { // get suffix, add one. If carry then make all zeros and 1 on the end int inc = slast.size() - scurrent.size(); ull pw = pow(10, inc); ull suffix = last - current * pw; suffix += 1; if (to_string(suffix).size() > slast.size() - scurrent.size()) { result += inc + 1; current = current * pw * 10; } else { result += inc; current = current * pw + suffix; } } else { int ctr = slast.size() - scurrent.size(); if (compared > 0) { ctr++; } for (int i=0; i<ctr; ++i) { current *= 10; } result += ctr; } } } last = current; lastBigStr = to_string(last); lastBigStrSize = lastBigStr.size(); } else { scurrent = to_string(current); // cout << "DBG " << lastBigStr << " :: " << lastBigStrSize << " -> " << current << endl; int compared = do_compare_big(/*slast, scurrent*/); result += lastBigStrSize - scurrent.size(); if (compared > 0) { result++; lastBigStrSize++; } if (compared != 0) { cpx(); } } } cout << result << endl; 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 | // Author: Krzysztof Bochenek // Email: kpbochenek@gmail.com // -------------------------------- #include <stdio.h> #include <vector> #include <map> #include <set> #include <stack> #include <math.h> #include <algorithm> #include <string> #include <iostream> #include <climits> #define pb push_back #define mp make_pair using namespace std; typedef long long int ll; typedef unsigned long long ull; #define rep(i, n) for(int i=0; i<n; ++i) int a[200001]; string slast, scurrent; string lastBigStr; ull lastBigStrSize; int do_compare(/*slast, scurrent*/) { for (int i=0; i<scurrent.size(); ++i) { if (slast[i] < scurrent[i]) { return -1; } else if (slast[i] > scurrent[i]) { return 1; } } return 0; } int do_compare_big(/*lastBigStr, scurrent*/) { for (int i=0; i<scurrent.size(); ++i) { if (lastBigStr[i] < scurrent[i]) { return -1; } else if (lastBigStr[i] > scurrent[i]) { return 1; } } return 0; } void cpx() { for (int i=0; i<20; ++i) { if (i < scurrent.size()) { lastBigStr[i] = scurrent[i]; } else { lastBigStr[i] = '0'; } } } int main() { ios::sync_with_stdio(0); ull result = 0; int N; cin >> N; for (int i=0; i<N; ++i) { cin >> a[i]; } ull last = 0; for (int i=0; i<N; ++i) { ull current = a[i]; if (last < 1000000000000000000) { if (last >= current) { slast = to_string(last); scurrent = to_string(current); if (slast.size() == scurrent.size()) { current *= 10; result++; } else { int compared = do_compare(/*slast, scurrent*/); if (compared == 0) { // get suffix, add one. If carry then make all zeros and 1 on the end int inc = slast.size() - scurrent.size(); ull pw = pow(10, inc); ull suffix = last - current * pw; suffix += 1; if (to_string(suffix).size() > slast.size() - scurrent.size()) { result += inc + 1; current = current * pw * 10; } else { result += inc; current = current * pw + suffix; } } else { int ctr = slast.size() - scurrent.size(); if (compared > 0) { ctr++; } for (int i=0; i<ctr; ++i) { current *= 10; } result += ctr; } } } last = current; lastBigStr = to_string(last); lastBigStrSize = lastBigStr.size(); } else { scurrent = to_string(current); // cout << "DBG " << lastBigStr << " :: " << lastBigStrSize << " -> " << current << endl; int compared = do_compare_big(/*slast, scurrent*/); result += lastBigStrSize - scurrent.size(); if (compared > 0) { result++; lastBigStrSize++; } if (compared != 0) { cpx(); } } } cout << result << endl; return 0; } |