#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> #include <map> #include <bits/stdc++.h> using namespace std; #define int long long int added_len = 0; string ne(string s) { int ix = s.size()-1; while(ix >= 0 && s[ix] == '9') --ix; if(ix == -1) return string{}; s[ix]++; ix++; while(ix != s.size()) { s[ix] = '0'; ++ix; } return s; } pair<string, int> f(string a, string b) { if(b.size() > a.size() || (a.size() == b.size() && b > a)) return {b, 0}; if(a.size() == b.size()) { b.push_back('0'); return {b, 1}; } string c = a.substr(0, b.size()); if(b > c) { int res = 0; while(b.size() < a.size() && b.size() < 25) { b.push_back('0'); ++res; } added_len += a.size() - b.size(); return {b, res}; } else if(b == c) { string u = a.substr(b.size()); string r = ne(u); if(r.size() != 0) return {b + r, r.size()}; } int res = 0; while(b.size() <= a.size() && b.size() < 25) { b.push_back('0'); ++res; } added_len += a.size() - b.size() + 1; return {b, res}; } int32_t main() { ios::sync_with_stdio(0); vector<string> v; int n; cin >> n; for(int i=0; i<n; i++) {int a; cin >> a; v.push_back(to_string(a));} int result = 0; string prev = v[0]; for(int i=1; i<n; i++) { pair<string, int> p = f(prev, v[i]); result += p.second + added_len; prev = p.first; } cout << result << endl; }
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <set> #include <map> #include <bits/stdc++.h> using namespace std; #define int long long int added_len = 0; string ne(string s) { int ix = s.size()-1; while(ix >= 0 && s[ix] == '9') --ix; if(ix == -1) return string{}; s[ix]++; ix++; while(ix != s.size()) { s[ix] = '0'; ++ix; } return s; } pair<string, int> f(string a, string b) { if(b.size() > a.size() || (a.size() == b.size() && b > a)) return {b, 0}; if(a.size() == b.size()) { b.push_back('0'); return {b, 1}; } string c = a.substr(0, b.size()); if(b > c) { int res = 0; while(b.size() < a.size() && b.size() < 25) { b.push_back('0'); ++res; } added_len += a.size() - b.size(); return {b, res}; } else if(b == c) { string u = a.substr(b.size()); string r = ne(u); if(r.size() != 0) return {b + r, r.size()}; } int res = 0; while(b.size() <= a.size() && b.size() < 25) { b.push_back('0'); ++res; } added_len += a.size() - b.size() + 1; return {b, res}; } int32_t main() { ios::sync_with_stdio(0); vector<string> v; int n; cin >> n; for(int i=0; i<n; i++) {int a; cin >> a; v.push_back(to_string(a));} int result = 0; string prev = v[0]; for(int i=1; i<n; i++) { pair<string, int> p = f(prev, v[i]); result += p.second + added_len; prev = p.first; } cout << result << endl; } |