#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto &i : a) #define SZ(i) ((int)(i).size()) #define X first #define Y second #define PR std::pair #define PII std::pair<int, int> #define MP std::make_pair #define ll long long #define VI std::vector<int> ll ans; int n; std::vector<std::string> A; std::string upgrade(std::string ret, int upto){ bool good = false; for(int i = SZ(ret)-1; i >= upto; --i){ if(ret[i] != '9'){ good = true; break; } } if(!good){ std::string xd = ret.substr(0, upto); REP(i, upto, SZ(ret)+1) xd.push_back('0'); return xd; } for(int i = SZ(ret)-1; i >= 0; --i){ if(ret[i] == '9') ret[i] = '0'; else{ ret[i] = (char)(ret[i]+1); return ret; } } throw std::runtime_error("Co jest"); } int what(const std::string &str, const std::string &was){ if(SZ(str) > SZ(was)) return 2; if(SZ(str) == SZ(was) && str > was) return 2; FOR(i, SZ(str)){ if(str[i] < was[i]) return -1; if(str[i] > was[i]) return 1; } return 0; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n; A.resize(n); FOR(i, n) std::cin >> A[i]; std::string last = A[0]; int len_last = SZ(A[0]); REP(i, 1, n){ int wh = what(A[i], last); if(SZ(last) < 30){ if(wh == 2){ last = A[i]; len_last = SZ(A[i]); }else if(wh == -1){ ans += len_last - SZ(A[i]) + 1; while(SZ(A[i]) <= len_last) A[i].push_back('0'); last = A[i]; len_last = SZ(A[i]); }else if(wh == 0){ ans += len_last - SZ(A[i]); A[i] = upgrade(last, SZ(A[i])); if(SZ(A[i]) > SZ(last)) ans++; last = A[i]; len_last = SZ(A[i]); }else if(wh == 1){ ans += len_last - SZ(A[i]); while(SZ(A[i]) < len_last) A[i].push_back('0'); last = A[i]; len_last = SZ(A[i]); } }else{ if(wh == 2){ last = A[i]; len_last = SZ(A[i]); }else if(wh == -1){ ans += len_last - SZ(A[i]) + 1; while(SZ(A[i]) < 30) A[i].push_back('0'); len_last++; last = A[i]; }else if(wh == 0 || wh == 1){ ans += len_last - SZ(A[i]); while(SZ(A[i]) < 30) A[i].push_back(wh == 1 ? '0' : last[SZ(A[i])]); last = A[i]; } } // std::cout << last << "\n"; } std::cout << ans << "\n"; 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 <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto &i : a) #define SZ(i) ((int)(i).size()) #define X first #define Y second #define PR std::pair #define PII std::pair<int, int> #define MP std::make_pair #define ll long long #define VI std::vector<int> ll ans; int n; std::vector<std::string> A; std::string upgrade(std::string ret, int upto){ bool good = false; for(int i = SZ(ret)-1; i >= upto; --i){ if(ret[i] != '9'){ good = true; break; } } if(!good){ std::string xd = ret.substr(0, upto); REP(i, upto, SZ(ret)+1) xd.push_back('0'); return xd; } for(int i = SZ(ret)-1; i >= 0; --i){ if(ret[i] == '9') ret[i] = '0'; else{ ret[i] = (char)(ret[i]+1); return ret; } } throw std::runtime_error("Co jest"); } int what(const std::string &str, const std::string &was){ if(SZ(str) > SZ(was)) return 2; if(SZ(str) == SZ(was) && str > was) return 2; FOR(i, SZ(str)){ if(str[i] < was[i]) return -1; if(str[i] > was[i]) return 1; } return 0; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n; A.resize(n); FOR(i, n) std::cin >> A[i]; std::string last = A[0]; int len_last = SZ(A[0]); REP(i, 1, n){ int wh = what(A[i], last); if(SZ(last) < 30){ if(wh == 2){ last = A[i]; len_last = SZ(A[i]); }else if(wh == -1){ ans += len_last - SZ(A[i]) + 1; while(SZ(A[i]) <= len_last) A[i].push_back('0'); last = A[i]; len_last = SZ(A[i]); }else if(wh == 0){ ans += len_last - SZ(A[i]); A[i] = upgrade(last, SZ(A[i])); if(SZ(A[i]) > SZ(last)) ans++; last = A[i]; len_last = SZ(A[i]); }else if(wh == 1){ ans += len_last - SZ(A[i]); while(SZ(A[i]) < len_last) A[i].push_back('0'); last = A[i]; len_last = SZ(A[i]); } }else{ if(wh == 2){ last = A[i]; len_last = SZ(A[i]); }else if(wh == -1){ ans += len_last - SZ(A[i]) + 1; while(SZ(A[i]) < 30) A[i].push_back('0'); len_last++; last = A[i]; }else if(wh == 0 || wh == 1){ ans += len_last - SZ(A[i]); while(SZ(A[i]) < 30) A[i].push_back(wh == 1 ? '0' : last[SZ(A[i])]); last = A[i]; } } // std::cout << last << "\n"; } std::cout << ans << "\n"; return 0; } |