#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { long n; cin >> n; vector<string> v; string x; for(long i = 0; i < n; i++) { cin >> x; v.push_back(x); } string lastnum; long long digits = 0; for(long i = 0; i < v.size(); i++) { if(i == 0) { lastnum = v[i]; } if(i > 0) { if(v[i].size() > lastnum.size()) { // this is fine! lastnum = v[i]; } else if (v[i].size() < lastnum.size()) { // need to add zeroes string newnum = v[i]; // check here if not one more zero! or maybe mutate last part to something greater ??? this is hard // break into prefix and suffix here string prefix = lastnum.substr(0, newnum.size()); if(newnum > prefix) { newnum.append(lastnum.size() - v[i].size(), '0'); digits += lastnum.size() - v[i].size(); } else if (newnum < prefix) { newnum.append(lastnum.size() - v[i].size() + 1, '0'); digits += lastnum.size() - v[i].size() + 1; } else { string suffix = lastnum.substr(newnum.size(), lastnum.size() - newnum.size()); // check if not all nines string allnines = ""; allnines = allnines.append(suffix.size(), '9'); if(suffix == allnines) { newnum.append(lastnum.size() - v[i].size() + 1, '0'); digits += lastnum.size() - v[i].size() + 1; } else { digits += lastnum.size() - v[i].size(); string newsuffix = suffix; for(long k = newsuffix.size() - 1; k >= 0; k--) { if(newsuffix[k] == '9') { newsuffix[k] = '0'; } else { newsuffix[k] = newsuffix[k] + 1; break; } } newnum = prefix + newsuffix; } } lastnum = newnum; } else { if(v[i] > lastnum) { // this is fine! lastnum = v[i]; } else if (v[i] < lastnum) { // same length, but next smaller... // unfortunately, you can only add a zero here... string newnum = v[i]; newnum.append(1, '0'); digits += 1; lastnum = newnum; } else { // unfortunately, you can only add a zero here... string newnum = v[i]; newnum.append(1, '0'); digits += 1; lastnum = newnum; } } } } cout << digits << 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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { long n; cin >> n; vector<string> v; string x; for(long i = 0; i < n; i++) { cin >> x; v.push_back(x); } string lastnum; long long digits = 0; for(long i = 0; i < v.size(); i++) { if(i == 0) { lastnum = v[i]; } if(i > 0) { if(v[i].size() > lastnum.size()) { // this is fine! lastnum = v[i]; } else if (v[i].size() < lastnum.size()) { // need to add zeroes string newnum = v[i]; // check here if not one more zero! or maybe mutate last part to something greater ??? this is hard // break into prefix and suffix here string prefix = lastnum.substr(0, newnum.size()); if(newnum > prefix) { newnum.append(lastnum.size() - v[i].size(), '0'); digits += lastnum.size() - v[i].size(); } else if (newnum < prefix) { newnum.append(lastnum.size() - v[i].size() + 1, '0'); digits += lastnum.size() - v[i].size() + 1; } else { string suffix = lastnum.substr(newnum.size(), lastnum.size() - newnum.size()); // check if not all nines string allnines = ""; allnines = allnines.append(suffix.size(), '9'); if(suffix == allnines) { newnum.append(lastnum.size() - v[i].size() + 1, '0'); digits += lastnum.size() - v[i].size() + 1; } else { digits += lastnum.size() - v[i].size(); string newsuffix = suffix; for(long k = newsuffix.size() - 1; k >= 0; k--) { if(newsuffix[k] == '9') { newsuffix[k] = '0'; } else { newsuffix[k] = newsuffix[k] + 1; break; } } newnum = prefix + newsuffix; } } lastnum = newnum; } else { if(v[i] > lastnum) { // this is fine! lastnum = v[i]; } else if (v[i] < lastnum) { // same length, but next smaller... // unfortunately, you can only add a zero here... string newnum = v[i]; newnum.append(1, '0'); digits += 1; lastnum = newnum; } else { // unfortunately, you can only add a zero here... string newnum = v[i]; newnum.append(1, '0'); digits += 1; lastnum = newnum; } } } } cout << digits << endl; return 0; } |