#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; using ll = unsigned long long int; int main() { ios::sync_with_stdio(0); int n; cin >> n; vector<string> numbers(n); vector<ll> nlenght(n); for (int i = 0; i < n; i++) { cin >> numbers[i]; } ll wyn = 0; for (int i = 1; i < n; i++) { string last = numbers[i-1]; if (nlenght[i-1]==0 && stoll(last) < stoll(numbers[i])) { continue; } else { if ( stoll(last.substr(0,numbers[i].length())) < stoll(numbers[i]) ) { ll added = last.length()+nlenght[i-1] - numbers[i].length(); wyn += added; for (int j = 0; j < added; j++) { if (numbers[i].length()>17) { nlenght[i] = added - j; break; } numbers[i] += '0'; } continue; } else { bool same = true; for (int j = 0; j < numbers[i].length(); j++) { if (last[j]!=numbers[i][j]) { same = false; break; } } if (same) { bool onlynine = true; for (int j = numbers[i].length(); j < last.length(); j++) { if (last[j]!='9') { onlynine = false; break; } } if (onlynine) { ll added = last.length() + nlenght[i - 1] - numbers[i].length() + 1; wyn += added; for (int j = 0; j < added; j++) { if (numbers[i].length() > 17) { nlenght[i] = added - j; break; } numbers[i] += '0'; } continue; } else { ll added = last.length() + nlenght[i - 1] - numbers[i].length(); wyn += added; ll lastval = stoll(last); numbers[i] = to_string(lastval+1); for (int j = 0; j < nlenght[i - 1]; j++) { if (numbers[i].length() > 17) { nlenght[i] = nlenght[i - 1] - j; break; } numbers[i] += '0'; } continue; } } else { ll added = last.length() + nlenght[i - 1] - numbers[i].length()+1; wyn += added; for (int j = 0; j < added; j++) { if (numbers[i].length() > 17) { nlenght[i] = added - j; break; } numbers[i] += '0'; } continue; } } } } cout << wyn << 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 | #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; using ll = unsigned long long int; int main() { ios::sync_with_stdio(0); int n; cin >> n; vector<string> numbers(n); vector<ll> nlenght(n); for (int i = 0; i < n; i++) { cin >> numbers[i]; } ll wyn = 0; for (int i = 1; i < n; i++) { string last = numbers[i-1]; if (nlenght[i-1]==0 && stoll(last) < stoll(numbers[i])) { continue; } else { if ( stoll(last.substr(0,numbers[i].length())) < stoll(numbers[i]) ) { ll added = last.length()+nlenght[i-1] - numbers[i].length(); wyn += added; for (int j = 0; j < added; j++) { if (numbers[i].length()>17) { nlenght[i] = added - j; break; } numbers[i] += '0'; } continue; } else { bool same = true; for (int j = 0; j < numbers[i].length(); j++) { if (last[j]!=numbers[i][j]) { same = false; break; } } if (same) { bool onlynine = true; for (int j = numbers[i].length(); j < last.length(); j++) { if (last[j]!='9') { onlynine = false; break; } } if (onlynine) { ll added = last.length() + nlenght[i - 1] - numbers[i].length() + 1; wyn += added; for (int j = 0; j < added; j++) { if (numbers[i].length() > 17) { nlenght[i] = added - j; break; } numbers[i] += '0'; } continue; } else { ll added = last.length() + nlenght[i - 1] - numbers[i].length(); wyn += added; ll lastval = stoll(last); numbers[i] = to_string(lastval+1); for (int j = 0; j < nlenght[i - 1]; j++) { if (numbers[i].length() > 17) { nlenght[i] = nlenght[i - 1] - j; break; } numbers[i] += '0'; } continue; } } else { ll added = last.length() + nlenght[i - 1] - numbers[i].length()+1; wyn += added; for (int j = 0; j < added; j++) { if (numbers[i].length() > 17) { nlenght[i] = added - j; break; } numbers[i] += '0'; } continue; } } } } cout << wyn << endl; return 0; } |