#include<cstdio> #include<string> #include<algorithm> using namespace std; int n; int a_num; class BigNum { private: string repr; public: BigNum(int a) : repr(to_string(a)) {} bool operator<(const string &s) { if(repr.size() == s.size()) { return repr < s; } return repr.size() < s.size(); } bool operator>(const string &s) { if(repr.size() == s.size()) { return repr > s; } return repr.size() > s.size(); } bool operator==(const string &s) { if(repr.size() == s.size()) { return repr == s; } return false; } void setRepr(const string &s) { repr = s; } bool isPrefEqual(const string &s) { return repr.substr(0, s.size()) == s; } bool isPrefBigger(const string &s) { return repr.substr(0, s.size()) > s; } int size() { return repr.size(); } bool onlyNinesAfterPrefix(const string &s) { for(int i = s.size(); i < repr.size(); i++) { if(repr[i] != '9') { return false; } } return true; } void addOne() { for(int i = repr.size() - 1; i >= 0; i--) { repr[i]++; if(repr[i] > '9') { repr[i] = '0'; } else { break; } } if(repr[0] == '0') { repr.insert(0, "1"); } } void setReprWithZeros(const string &s, int desired_length) { for(int i = 0; i < s.size(); i++) { repr[i] = s[i]; } int sent = min(int(repr.size()), 20); for(int i = s.size(); i < sent; i++) { repr[i] = '0'; } for(int i = (repr.size() - 1); i >= max(int(repr.size()) - 10, sent); i--) { repr[i] = '0'; } repr.append(desired_length - repr.size(), '0'); } void appendZero() { repr.append("0"); } }; string a; int main() { scanf("%d", &n); scanf("%d", &a_num); BigNum cur_salary(a_num); long long res = 0; for(int i = 1; i < n; i++) { scanf("%d", &a_num); a = to_string(a_num); if(cur_salary < a) { cur_salary.setRepr(a); } else if(cur_salary == a) { res++; cur_salary.appendZero(); } else { if(cur_salary.isPrefBigger(a)) { cur_salary.setReprWithZeros(a, cur_salary.size() + 1); res += (cur_salary.size() - a.size()); } else if(cur_salary.isPrefEqual(a)) { if(cur_salary.onlyNinesAfterPrefix(a)) { cur_salary.setReprWithZeros(a, cur_salary.size() + 1); res += (cur_salary.size() - a.size()); } else { cur_salary.addOne(); res += (cur_salary.size() - a.size()); } } else { cur_salary.setReprWithZeros(a, cur_salary.size()); res += (cur_salary.size() - a.size()); } } } printf("%lld\n", 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 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 | #include<cstdio> #include<string> #include<algorithm> using namespace std; int n; int a_num; class BigNum { private: string repr; public: BigNum(int a) : repr(to_string(a)) {} bool operator<(const string &s) { if(repr.size() == s.size()) { return repr < s; } return repr.size() < s.size(); } bool operator>(const string &s) { if(repr.size() == s.size()) { return repr > s; } return repr.size() > s.size(); } bool operator==(const string &s) { if(repr.size() == s.size()) { return repr == s; } return false; } void setRepr(const string &s) { repr = s; } bool isPrefEqual(const string &s) { return repr.substr(0, s.size()) == s; } bool isPrefBigger(const string &s) { return repr.substr(0, s.size()) > s; } int size() { return repr.size(); } bool onlyNinesAfterPrefix(const string &s) { for(int i = s.size(); i < repr.size(); i++) { if(repr[i] != '9') { return false; } } return true; } void addOne() { for(int i = repr.size() - 1; i >= 0; i--) { repr[i]++; if(repr[i] > '9') { repr[i] = '0'; } else { break; } } if(repr[0] == '0') { repr.insert(0, "1"); } } void setReprWithZeros(const string &s, int desired_length) { for(int i = 0; i < s.size(); i++) { repr[i] = s[i]; } int sent = min(int(repr.size()), 20); for(int i = s.size(); i < sent; i++) { repr[i] = '0'; } for(int i = (repr.size() - 1); i >= max(int(repr.size()) - 10, sent); i--) { repr[i] = '0'; } repr.append(desired_length - repr.size(), '0'); } void appendZero() { repr.append("0"); } }; string a; int main() { scanf("%d", &n); scanf("%d", &a_num); BigNum cur_salary(a_num); long long res = 0; for(int i = 1; i < n; i++) { scanf("%d", &a_num); a = to_string(a_num); if(cur_salary < a) { cur_salary.setRepr(a); } else if(cur_salary == a) { res++; cur_salary.appendZero(); } else { if(cur_salary.isPrefBigger(a)) { cur_salary.setReprWithZeros(a, cur_salary.size() + 1); res += (cur_salary.size() - a.size()); } else if(cur_salary.isPrefEqual(a)) { if(cur_salary.onlyNinesAfterPrefix(a)) { cur_salary.setReprWithZeros(a, cur_salary.size() + 1); res += (cur_salary.size() - a.size()); } else { cur_salary.addOne(); res += (cur_salary.size() - a.size()); } } else { cur_salary.setReprWithZeros(a, cur_salary.size()); res += (cur_salary.size() - a.size()); } } } printf("%lld\n", res); return 0; } |