#include <iostream> #include <string> #include <algorithm> using namespace std; const int maxN = 200001; int n; long long result = 0; bool changed[maxN]; string kubkedPrev; bool compare(string s1, string s2) { if (s1.size() < s2.size()) return true; else if (s1.size() > s2.size()) return false; return s1 < s2; } // po wpisaniu samych zer bool willBeBigger(string s1, string s2) { for (int i = 0; i < s1.size(); i++) { if (s1[i] < s2[i]) return false; if (s1[i] > s2[i]) return true; } return false; } bool willBeSmaller(string s1, string s2) { for (int i = 0; i < s1.size(); i++) { if (s1[i] < s2[i]) return true; if (s1[i] > s2[i]) return false; } return false; } bool willBeEqual(string s1, string s2) { for (int i = 0; i < s1.size(); i++) { if (s1[i] < s2[i]) return false; if (s1[i] > s2[i]) return false; } return true; } void check(string act) { // cout << "Check for prev = " << kubkedPrev << " act = " << act << endl; if (!compare(kubkedPrev, act)) { if (act.size() < kubkedPrev.size()) { if (willBeBigger(act, kubkedPrev)) { string tmp = act; for (int i = 0; i < min((int)kubkedPrev.length(), 10); i++) { kubkedPrev[i] = '0'; } for (int i = act.size() -1; i >= kubkedPrev.size() - 11; i--) { if (i < 0) break; kubkedPrev[i] = '0'; } for (int i = 0; i < tmp.size(); i++) { kubkedPrev[i] = tmp[i]; } result += (kubkedPrev.size() - tmp.size()); } else if (willBeEqual(act, kubkedPrev)) { string tmp = act; bool flag = false; for (int idx = kubkedPrev.size() - 1 ; idx >= tmp.size(); idx --) { if (kubkedPrev[idx] != '9') {flag = true;break;} } if (!flag) { kubkedPrev.append("0");// = act + "0"; for (int idx = kubkedPrev.size() - 2 ; idx >= tmp.size(); idx --) { if (kubkedPrev[idx] == '9') kubkedPrev[idx] = '0'; else break; } } else { for (int idx = kubkedPrev.size() - 1 ; idx >= tmp.size(); idx --) { if (kubkedPrev[idx] == '9') kubkedPrev[idx] = '0'; else { int val = (int(kubkedPrev[idx]) - 48) + 1; kubkedPrev[idx] = char(val + 48); break; } } } result += (kubkedPrev.size() - tmp.size()); } else { string tmp = act; for (int i = 0; i < min((int)kubkedPrev.length(), 10); i++) { kubkedPrev[i] = '0'; } for (int i = kubkedPrev.size() -1; i >= kubkedPrev.size() - 11; i--) { if (i < 0) break; kubkedPrev[i] = '0'; } for (int i = 0; i < tmp.size(); i++) { kubkedPrev[i] = tmp[i]; } kubkedPrev.append("0"); result += (kubkedPrev.size() - tmp.size()); } } else { // equal; kubkedPrev = act; kubkedPrev.append("0");// = act + "0"; result++; } } else { kubkedPrev = act; } } int main() { ios_base::sync_with_stdio(0); string s; cin >> n; cin >> kubkedPrev; for (int i = 0; i < n - 1; i++) { cin>>s; check(s); } for (int i = 0; i < n; i++) { // cout << t[i] << endl;; } cout << result; return 0; } // string prev = t[i - 1]; // string act = t[i]; // cout << "Check for prev = " << prev << " act = " << act << endl; // if (!compare(prev, act)) { // cout << " >= " << endl; // if (changed[i]) { // cout << "Changed = true" << endl; // if (act[act.size() - 1] == '9') { // act[act.size() - 1] = '0'; // act = act + "0"; // result ++; // cout << " == 9 " << endl; // } // else { // int val = (int(act[act.size() - 1]) - 48) + 1; // act[act.size() - 1] = char(val + 48); // cout << "Changed to " << char(val + 48) << endl; // } // } else { // cout << "Changed false" << endl; // act = act + "0"; // result ++; // changed[i] = true; // } // t[i] = act; // cout << "After prev = " << t[i-1] << " next = " << t[i] << endl; // check(i); // return; // }
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 | #include <iostream> #include <string> #include <algorithm> using namespace std; const int maxN = 200001; int n; long long result = 0; bool changed[maxN]; string kubkedPrev; bool compare(string s1, string s2) { if (s1.size() < s2.size()) return true; else if (s1.size() > s2.size()) return false; return s1 < s2; } // po wpisaniu samych zer bool willBeBigger(string s1, string s2) { for (int i = 0; i < s1.size(); i++) { if (s1[i] < s2[i]) return false; if (s1[i] > s2[i]) return true; } return false; } bool willBeSmaller(string s1, string s2) { for (int i = 0; i < s1.size(); i++) { if (s1[i] < s2[i]) return true; if (s1[i] > s2[i]) return false; } return false; } bool willBeEqual(string s1, string s2) { for (int i = 0; i < s1.size(); i++) { if (s1[i] < s2[i]) return false; if (s1[i] > s2[i]) return false; } return true; } void check(string act) { // cout << "Check for prev = " << kubkedPrev << " act = " << act << endl; if (!compare(kubkedPrev, act)) { if (act.size() < kubkedPrev.size()) { if (willBeBigger(act, kubkedPrev)) { string tmp = act; for (int i = 0; i < min((int)kubkedPrev.length(), 10); i++) { kubkedPrev[i] = '0'; } for (int i = act.size() -1; i >= kubkedPrev.size() - 11; i--) { if (i < 0) break; kubkedPrev[i] = '0'; } for (int i = 0; i < tmp.size(); i++) { kubkedPrev[i] = tmp[i]; } result += (kubkedPrev.size() - tmp.size()); } else if (willBeEqual(act, kubkedPrev)) { string tmp = act; bool flag = false; for (int idx = kubkedPrev.size() - 1 ; idx >= tmp.size(); idx --) { if (kubkedPrev[idx] != '9') {flag = true;break;} } if (!flag) { kubkedPrev.append("0");// = act + "0"; for (int idx = kubkedPrev.size() - 2 ; idx >= tmp.size(); idx --) { if (kubkedPrev[idx] == '9') kubkedPrev[idx] = '0'; else break; } } else { for (int idx = kubkedPrev.size() - 1 ; idx >= tmp.size(); idx --) { if (kubkedPrev[idx] == '9') kubkedPrev[idx] = '0'; else { int val = (int(kubkedPrev[idx]) - 48) + 1; kubkedPrev[idx] = char(val + 48); break; } } } result += (kubkedPrev.size() - tmp.size()); } else { string tmp = act; for (int i = 0; i < min((int)kubkedPrev.length(), 10); i++) { kubkedPrev[i] = '0'; } for (int i = kubkedPrev.size() -1; i >= kubkedPrev.size() - 11; i--) { if (i < 0) break; kubkedPrev[i] = '0'; } for (int i = 0; i < tmp.size(); i++) { kubkedPrev[i] = tmp[i]; } kubkedPrev.append("0"); result += (kubkedPrev.size() - tmp.size()); } } else { // equal; kubkedPrev = act; kubkedPrev.append("0");// = act + "0"; result++; } } else { kubkedPrev = act; } } int main() { ios_base::sync_with_stdio(0); string s; cin >> n; cin >> kubkedPrev; for (int i = 0; i < n - 1; i++) { cin>>s; check(s); } for (int i = 0; i < n; i++) { // cout << t[i] << endl;; } cout << result; return 0; } // string prev = t[i - 1]; // string act = t[i]; // cout << "Check for prev = " << prev << " act = " << act << endl; // if (!compare(prev, act)) { // cout << " >= " << endl; // if (changed[i]) { // cout << "Changed = true" << endl; // if (act[act.size() - 1] == '9') { // act[act.size() - 1] = '0'; // act = act + "0"; // result ++; // cout << " == 9 " << endl; // } // else { // int val = (int(act[act.size() - 1]) - 48) + 1; // act[act.size() - 1] = char(val + 48); // cout << "Changed to " << char(val + 48) << endl; // } // } else { // cout << "Changed false" << endl; // act = act + "0"; // result ++; // changed[i] = true; // } // t[i] = act; // cout << "After prev = " << t[i-1] << " next = " << t[i] << endl; // check(i); // return; // } |