#include <bits/stdc++.h> using namespace std; int getIntSize(int number) { if(number == 0) return 0; int res = getIntSize(number/10); return res + 1; } int allNine(int number) { if(number % 10 != 9) return false; if(number/10 == 0) return true; bool res = allNine(number/10); return res; } vector<char> getVecToCompare(int N, vector<char>& org, int zeroes, int addition) { int i = 0; vector<char> toCompare(N); for(; i < org.size() && i < N; i++) { toCompare[i] = org[i]; } for(; (i < org.size() + zeroes) && i < N; i++) { toCompare[i] = '0'; } int sizeAddition = getIntSize(addition); vector<char> helper; for(int j=i; (j < org.size() + zeroes + sizeAddition); j++) { helper.push_back((addition % 10) + '0'); addition /= 10; } std::reverse(helper.begin(), helper.end()); for(int j=0; i < N; j++, i++) toCompare[i] = helper[j]; return toCompare; } int getNumberCharToCompare(int origI, int origPrev, int zeroesPrev, int additionPrev) { if(origI <= origPrev + zeroesPrev + additionPrev) return origI; return origPrev + zeroesPrev + additionPrev; } int main() { std::ios::sync_with_stdio(false); int n, number, t, rest; cin >> n; vector<int> vec(n); vector<vector<char>> orig(n); vector<int> zeroes(n), addition(n); for(int i=0; i<n; i++) { cin >> vec[i]; number = vec[i]; while(number > 0) { rest = number%10; number = number/10; orig[i].push_back(rest + '0'); } std::reverse(orig[i].begin(), orig[i].end()); } zeroes[0] = 0; addition[0] = 0; long long result = 0; for(int i=1; i<n; i++) { int sizPrevToCompare = getNumberCharToCompare(orig[i].size(), orig[i-1].size(), zeroes[i-1], getIntSize(addition[i-1])); vector<char> toCompare = getVecToCompare(sizPrevToCompare, orig[i-1], zeroes[i-1], addition[i-1]); int state = -1, prevSize = orig[i-1].size() + zeroes[i-1] + getIntSize(addition[i-1]); if(orig[i].size() > prevSize) { addition[i] = 0; zeroes[i] = 0; continue; } for(int j=0; j<sizPrevToCompare; j++) { if(orig[i][j] > toCompare[j]) { state = 1; break; } else if(orig[i][j] < toCompare[j]) { state = 0; break; } } addition[i] = 0; if(state == 0) { // oryginalny jest mniejszy zeroes[i] = orig[i-1].size() + zeroes[i-1] + getIntSize(addition[i-1]) - orig[i].size() + 1; } else if(state == 1) { // oryginalny jest wiekszy, zeroes[i] = orig[i-1].size() + zeroes[i-1] + getIntSize(addition[i-1]) - orig[i].size(); } else { if(orig[i].size() < prevSize) { if(sizPrevToCompare < orig[i-1].size()) { // trzeba dopelnic np. dla 989 i 9, trzeba uzupelnic o 89 if(addition[i-1] == 0 && zeroes[i-1] == 0) { string sss(orig[i-1].begin()+sizPrevToCompare, orig[i-1].end()); int grr = std::stoi(sss); if(allNine(grr)) { zeroes[i] = orig[i-1].size() - sizPrevToCompare + 1; addition[i] = 0; } else { zeroes[i] = 0; addition[i] = grr + 1; } } else if(addition[i-1] == 0 && zeroes[i-1] != 0) { zeroes[i] = zeroes[i-1] - 1 + orig[i-1].size() - sizPrevToCompare; addition[i] = 1; } else if(addition[i-1] != 0 && zeroes[i-1] == 0) { string sss(orig[i-1].begin()+sizPrevToCompare, orig[i-1].end()); std::to_string(addition[i-1]); string super = sss + std::to_string(addition[i-1]); int grr = std::stoi(sss); if(allNine(grr+1)) { zeroes[i] = super.size() + 1; addition[i] = 0; } else { addition[i] = grr + 1; zeroes[i] = 0; } } else { for(int kk=sizPrevToCompare; kk<orig[i-1].size(); kk++) // oj, tutaj bedzie wiecej roboty orig[i].push_back(orig[i-1][kk]); result += orig[i-1].size() + sizPrevToCompare; if(allNine(addition[i-1] + 1)) { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1] - 1; } else { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1]; } } result += getIntSize(addition[i]); } else { if(addition[i-1] == 0 && zeroes[i-1] == 0) { zeroes[i] = 1; } else if(addition[i-1] == 0 && zeroes[i-1] != 0) { zeroes[i] = zeroes[i-1] - 1; addition[i] = 1; } else if(addition[i-1] != 0 && zeroes[i-1] == 0) { if(allNine(addition[i-1])) { zeroes[i] = zeroes[i-1] + getIntSize(addition[i-1]) + 1; addition[i] = 0; } else { zeroes[i] = 0; addition[i] = addition[i-1] + 1; } } else { if(allNine(addition[i-1])) { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1] - 1; } else { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1]; } } result += getIntSize(addition[i]); } } else if(orig[i].size() == prevSize) { vector<char> final_test = getVecToCompare(prevSize, orig[i-1], zeroes[i-1], addition[i-1]); int state = 0; for(int lol=0; lol<prevSize; lol++) { if(final_test[lol] > orig[i][lol]) { state = -1; break; } if(final_test[lol] < orig[i][lol]) { state = 1; break; } } if(state == -1) { zeroes[i] = zeroes[i-1] + 1; } else if(state == 0) { zeroes[i] = zeroes[i-1] + 1; // samo 1? } else { zeroes[i] = 0; } } else { zeroes[i] = 0; } } result += zeroes[i]; } 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #include <bits/stdc++.h> using namespace std; int getIntSize(int number) { if(number == 0) return 0; int res = getIntSize(number/10); return res + 1; } int allNine(int number) { if(number % 10 != 9) return false; if(number/10 == 0) return true; bool res = allNine(number/10); return res; } vector<char> getVecToCompare(int N, vector<char>& org, int zeroes, int addition) { int i = 0; vector<char> toCompare(N); for(; i < org.size() && i < N; i++) { toCompare[i] = org[i]; } for(; (i < org.size() + zeroes) && i < N; i++) { toCompare[i] = '0'; } int sizeAddition = getIntSize(addition); vector<char> helper; for(int j=i; (j < org.size() + zeroes + sizeAddition); j++) { helper.push_back((addition % 10) + '0'); addition /= 10; } std::reverse(helper.begin(), helper.end()); for(int j=0; i < N; j++, i++) toCompare[i] = helper[j]; return toCompare; } int getNumberCharToCompare(int origI, int origPrev, int zeroesPrev, int additionPrev) { if(origI <= origPrev + zeroesPrev + additionPrev) return origI; return origPrev + zeroesPrev + additionPrev; } int main() { std::ios::sync_with_stdio(false); int n, number, t, rest; cin >> n; vector<int> vec(n); vector<vector<char>> orig(n); vector<int> zeroes(n), addition(n); for(int i=0; i<n; i++) { cin >> vec[i]; number = vec[i]; while(number > 0) { rest = number%10; number = number/10; orig[i].push_back(rest + '0'); } std::reverse(orig[i].begin(), orig[i].end()); } zeroes[0] = 0; addition[0] = 0; long long result = 0; for(int i=1; i<n; i++) { int sizPrevToCompare = getNumberCharToCompare(orig[i].size(), orig[i-1].size(), zeroes[i-1], getIntSize(addition[i-1])); vector<char> toCompare = getVecToCompare(sizPrevToCompare, orig[i-1], zeroes[i-1], addition[i-1]); int state = -1, prevSize = orig[i-1].size() + zeroes[i-1] + getIntSize(addition[i-1]); if(orig[i].size() > prevSize) { addition[i] = 0; zeroes[i] = 0; continue; } for(int j=0; j<sizPrevToCompare; j++) { if(orig[i][j] > toCompare[j]) { state = 1; break; } else if(orig[i][j] < toCompare[j]) { state = 0; break; } } addition[i] = 0; if(state == 0) { // oryginalny jest mniejszy zeroes[i] = orig[i-1].size() + zeroes[i-1] + getIntSize(addition[i-1]) - orig[i].size() + 1; } else if(state == 1) { // oryginalny jest wiekszy, zeroes[i] = orig[i-1].size() + zeroes[i-1] + getIntSize(addition[i-1]) - orig[i].size(); } else { if(orig[i].size() < prevSize) { if(sizPrevToCompare < orig[i-1].size()) { // trzeba dopelnic np. dla 989 i 9, trzeba uzupelnic o 89 if(addition[i-1] == 0 && zeroes[i-1] == 0) { string sss(orig[i-1].begin()+sizPrevToCompare, orig[i-1].end()); int grr = std::stoi(sss); if(allNine(grr)) { zeroes[i] = orig[i-1].size() - sizPrevToCompare + 1; addition[i] = 0; } else { zeroes[i] = 0; addition[i] = grr + 1; } } else if(addition[i-1] == 0 && zeroes[i-1] != 0) { zeroes[i] = zeroes[i-1] - 1 + orig[i-1].size() - sizPrevToCompare; addition[i] = 1; } else if(addition[i-1] != 0 && zeroes[i-1] == 0) { string sss(orig[i-1].begin()+sizPrevToCompare, orig[i-1].end()); std::to_string(addition[i-1]); string super = sss + std::to_string(addition[i-1]); int grr = std::stoi(sss); if(allNine(grr+1)) { zeroes[i] = super.size() + 1; addition[i] = 0; } else { addition[i] = grr + 1; zeroes[i] = 0; } } else { for(int kk=sizPrevToCompare; kk<orig[i-1].size(); kk++) // oj, tutaj bedzie wiecej roboty orig[i].push_back(orig[i-1][kk]); result += orig[i-1].size() + sizPrevToCompare; if(allNine(addition[i-1] + 1)) { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1] - 1; } else { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1]; } } result += getIntSize(addition[i]); } else { if(addition[i-1] == 0 && zeroes[i-1] == 0) { zeroes[i] = 1; } else if(addition[i-1] == 0 && zeroes[i-1] != 0) { zeroes[i] = zeroes[i-1] - 1; addition[i] = 1; } else if(addition[i-1] != 0 && zeroes[i-1] == 0) { if(allNine(addition[i-1])) { zeroes[i] = zeroes[i-1] + getIntSize(addition[i-1]) + 1; addition[i] = 0; } else { zeroes[i] = 0; addition[i] = addition[i-1] + 1; } } else { if(allNine(addition[i-1])) { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1] - 1; } else { addition[i] = addition[i-1] + 1; zeroes[i] = zeroes[i-1]; } } result += getIntSize(addition[i]); } } else if(orig[i].size() == prevSize) { vector<char> final_test = getVecToCompare(prevSize, orig[i-1], zeroes[i-1], addition[i-1]); int state = 0; for(int lol=0; lol<prevSize; lol++) { if(final_test[lol] > orig[i][lol]) { state = -1; break; } if(final_test[lol] < orig[i][lol]) { state = 1; break; } } if(state == -1) { zeroes[i] = zeroes[i-1] + 1; } else if(state == 0) { zeroes[i] = zeroes[i-1] + 1; // samo 1? } else { zeroes[i] = 0; } } else { zeroes[i] = 0; } } result += zeroes[i]; } cout << result << endl; return 0; } |