//Piotr Golda #include <iostream> #include <vector> #include <set> #include <algorithm> using ANumberT = std::vector < unsigned char > ; using AContainerT = std::vector < ANumberT > ; void rewriteNumber(ANumberT& aNumber, unsigned int a) { if (a != 0) { rewriteNumber(aNumber, a / 10); aNumber.push_back(a % 10); } } AContainerT readInput() { size_t N; AContainerT A; unsigned int a; std::cin >> N; A.resize(N); for (auto& aNumber : A) { std::cin >> a; rewriteNumber(aNumber, a); } return A; } void appendZeros(ANumberT& list, ANumberT::size_type count) { list.insert(list.end(), count, ANumberT::value_type{ 0 }); } enum class CMP { EQUAL, GREATER, LOWER }; void increment(ANumberT& lastNumber, const size_t lastNonChangableDigit, std::set<size_t>& nonZeros) { size_t i{ 0 }; for (i = lastNumber.size() - 1; i > lastNonChangableDigit; --i) { if (lastNumber[i] != 9) { ++lastNumber[i]; nonZeros.insert(i); return; } lastNumber[i] = 0; } lastNumber.push_back(0); } unsigned long long int compute( const AContainerT& A) { unsigned long long int result{ 0 }; ANumberT lastNumber{ A[0] }; std::set<size_t> nonZeros; for (size_t i = 0; i < lastNumber.size(); ++i) if (lastNumber[i] != 0) nonZeros.insert(i); for (size_t i = 1; i < A.size(); ++i) { //std::cout << "comparing: " << lastNumber << " with " << A[i] << std::endl; if (lastNumber.size() < A[i].size()) { lastNumber = A[i]; nonZeros.clear(); for (size_t i = 0; i < lastNumber.size(); ++i) if (lastNumber[i] != 0) nonZeros.insert(i); continue; } CMP cmp{ CMP::EQUAL }; size_t idx{ 0 }; for (idx = 0; idx < A[i].size(); ++idx) { if (cmp == CMP::EQUAL) { if (lastNumber[idx] < A[i][idx]) cmp = CMP::LOWER; else if (lastNumber[idx] > A[i][idx]) cmp = CMP::GREATER; } lastNumber[idx] = A[i][idx]; if (lastNumber[idx] != 0) nonZeros.insert(idx); } if (cmp == CMP::EQUAL) { increment(lastNumber, A[i].size() - 1, nonZeros); } else { if (cmp == CMP::GREATER) { lastNumber.push_back(0); } auto it = nonZeros.rbegin(); while (it != nonZeros.rend() && *it > A[i].size() - 1) { lastNumber[*it] = 0; nonZeros.erase(*it); } } result += (lastNumber.size() > A[i].size()) ? lastNumber.size() - A[i].size() : 0; } return result; } int main() { std::ios_base::sync_with_stdio(false); auto A = readInput(); std::cout << compute(A) << std::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 | //Piotr Golda #include <iostream> #include <vector> #include <set> #include <algorithm> using ANumberT = std::vector < unsigned char > ; using AContainerT = std::vector < ANumberT > ; void rewriteNumber(ANumberT& aNumber, unsigned int a) { if (a != 0) { rewriteNumber(aNumber, a / 10); aNumber.push_back(a % 10); } } AContainerT readInput() { size_t N; AContainerT A; unsigned int a; std::cin >> N; A.resize(N); for (auto& aNumber : A) { std::cin >> a; rewriteNumber(aNumber, a); } return A; } void appendZeros(ANumberT& list, ANumberT::size_type count) { list.insert(list.end(), count, ANumberT::value_type{ 0 }); } enum class CMP { EQUAL, GREATER, LOWER }; void increment(ANumberT& lastNumber, const size_t lastNonChangableDigit, std::set<size_t>& nonZeros) { size_t i{ 0 }; for (i = lastNumber.size() - 1; i > lastNonChangableDigit; --i) { if (lastNumber[i] != 9) { ++lastNumber[i]; nonZeros.insert(i); return; } lastNumber[i] = 0; } lastNumber.push_back(0); } unsigned long long int compute( const AContainerT& A) { unsigned long long int result{ 0 }; ANumberT lastNumber{ A[0] }; std::set<size_t> nonZeros; for (size_t i = 0; i < lastNumber.size(); ++i) if (lastNumber[i] != 0) nonZeros.insert(i); for (size_t i = 1; i < A.size(); ++i) { //std::cout << "comparing: " << lastNumber << " with " << A[i] << std::endl; if (lastNumber.size() < A[i].size()) { lastNumber = A[i]; nonZeros.clear(); for (size_t i = 0; i < lastNumber.size(); ++i) if (lastNumber[i] != 0) nonZeros.insert(i); continue; } CMP cmp{ CMP::EQUAL }; size_t idx{ 0 }; for (idx = 0; idx < A[i].size(); ++idx) { if (cmp == CMP::EQUAL) { if (lastNumber[idx] < A[i][idx]) cmp = CMP::LOWER; else if (lastNumber[idx] > A[i][idx]) cmp = CMP::GREATER; } lastNumber[idx] = A[i][idx]; if (lastNumber[idx] != 0) nonZeros.insert(idx); } if (cmp == CMP::EQUAL) { increment(lastNumber, A[i].size() - 1, nonZeros); } else { if (cmp == CMP::GREATER) { lastNumber.push_back(0); } auto it = nonZeros.rbegin(); while (it != nonZeros.rend() && *it > A[i].size() - 1) { lastNumber[*it] = 0; nonZeros.erase(*it); } } result += (lastNumber.size() > A[i].size()) ? lastNumber.size() - A[i].size() : 0; } return result; } int main() { std::ios_base::sync_with_stdio(false); auto A = readInput(); std::cout << compute(A) << std::endl; return 0; } |