#include <iostream> #include <vector> #include <tuple> #include <cmath> #include <string> using namespace std; string add_one(string a){ a[a.length() - 1] += 1; for(int i=a.length() - 1; i>0; i--){ if((int)a[i] > 57){ a[i - 1] += 1; a[i] -= 10; } else{ break; } } if((int)a[0] > 57){ a[0] -= 10; a = '1' + a; } return a; } bool is_greater(string a, string b){ if(a.length() > b.length()){ return true; } if(a.length() < b.length()){ return false; } for(int i=0;i<a.length();i++){ if((int)a[i] > (int)b[i]){ return true; } if((int)a[i] < (int)b[i]){ return false; } } return false; } tuple<unsigned long, string> make_greater(string a, const string &b) { unsigned long res = 0; string sub = b.substr(0, a.length()); if(is_greater(a, sub)){ res = b.length() - a.length(); string zeros; zeros.resize(res, '0'); return make_tuple(res, a.append(zeros)); } if(is_greater(sub, a)){ res = b.length() - a.length() + 1; string zeros; zeros.resize(res, '0'); return make_tuple(res, a.append(zeros)); } string temp = add_one(b.substr(a.length(), b.length() - a.length())); if(b.length() - a.length() < temp.length()){ res = b.length() - a.length() + 1; string zeros; zeros.resize(res, '0'); return make_tuple(res, a.append(zeros)); } else{ res = b.length() - a.length(); return make_tuple(res, a.append(temp)); } } int main() { ios_base::sync_with_stdio(0); unsigned long n; cin >> n; vector<string> vec(n); long long int res = 0; for (unsigned long i = 0; i < n; i++) { cin >> vec[i]; } string a; long long int r; for (unsigned long i = 1; i < n; i++) { if (!is_greater(vec[i], vec[i-1])) { tie(r, a) = make_greater(vec[i], vec[i-1]); res += r; vec[i] = a; } } cout << res; 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 | #include <iostream> #include <vector> #include <tuple> #include <cmath> #include <string> using namespace std; string add_one(string a){ a[a.length() - 1] += 1; for(int i=a.length() - 1; i>0; i--){ if((int)a[i] > 57){ a[i - 1] += 1; a[i] -= 10; } else{ break; } } if((int)a[0] > 57){ a[0] -= 10; a = '1' + a; } return a; } bool is_greater(string a, string b){ if(a.length() > b.length()){ return true; } if(a.length() < b.length()){ return false; } for(int i=0;i<a.length();i++){ if((int)a[i] > (int)b[i]){ return true; } if((int)a[i] < (int)b[i]){ return false; } } return false; } tuple<unsigned long, string> make_greater(string a, const string &b) { unsigned long res = 0; string sub = b.substr(0, a.length()); if(is_greater(a, sub)){ res = b.length() - a.length(); string zeros; zeros.resize(res, '0'); return make_tuple(res, a.append(zeros)); } if(is_greater(sub, a)){ res = b.length() - a.length() + 1; string zeros; zeros.resize(res, '0'); return make_tuple(res, a.append(zeros)); } string temp = add_one(b.substr(a.length(), b.length() - a.length())); if(b.length() - a.length() < temp.length()){ res = b.length() - a.length() + 1; string zeros; zeros.resize(res, '0'); return make_tuple(res, a.append(zeros)); } else{ res = b.length() - a.length(); return make_tuple(res, a.append(temp)); } } int main() { ios_base::sync_with_stdio(0); unsigned long n; cin >> n; vector<string> vec(n); long long int res = 0; for (unsigned long i = 0; i < n; i++) { cin >> vec[i]; } string a; long long int r; for (unsigned long i = 1; i < n; i++) { if (!is_greater(vec[i], vec[i-1])) { tie(r, a) = make_greater(vec[i], vec[i-1]); res += r; vec[i] = a; } } cout << res; return 0; } |