#include <bits/stdc++.h> #include <string> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define st first #define nd second #define pb push_back #define REP(i,a,b) for(int i = (a); i <= (b); i++) #define FOR(i, n) REP(i, 0, int(n) - 1) #define pii pair < int, int > #define ll long long #define fi first #define se second #define mp make_pair #define vi vector <int> vector<string> v; vector<int> zeros; ll RES = 0; const int MAXL = 18; string tos(int x) { string res = ""; while(x) { res += (x % 10) + '0'; x /= 10; } reverse(res.begin(), res.end()); return res; } string bigger(string a, string b, int i) { int n = a.size(), k = b.size(); zeros[i] = zeros[i-1]; if(n < k) ; else if(n == k && a < b) ; else if(n == k) b += '0'; else if(a.substr(0,k) < b) { FOR(i,n-k) b += '0'; } else if(a.substr(0,k) > b) { FOR(i,n-k) b += '0'; if(b.size() < MAXL) b += '0'; else zeros[i]++; } //puts("*"); else { if(a.size() == MAXL) b = a; else { bool all9 = 1; REP(i,k,n-1) if(a[i] != 9) all9 = 0; if(all9) FOR(i,n-k+1) b += '0'; else { string toadd = ""; bool added = 0; for(int i = n-1; i >= k; i--) { if(a[i] == '9' && added == 0) toadd += '0'; else if (added == 0) { toadd += a[i] + 1; added = 1; } else toadd += a[i]; } reverse(toadd.begin(), toadd.end()); //cout<<"toadd "<<toadd<<"\n"; if(added == 1) b += toadd; else FOR(i,n-k+1) b += '0'; } } } RES += b.size() - k + zeros[i]; return b; } int main() { ios_base::sync_with_stdio(0); int n; cin>>n; FOR(i,n) { int a; cin>>a; v.pb(tos(a)); zeros.pb(0); } /*FOR(i,n) { cout<<v[i]<<"\n"; }*/ REP(i,1,n-1) v[i] = bigger(v[i-1], v[i], i); cout<<RES<<"\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> #include <string> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define st first #define nd second #define pb push_back #define REP(i,a,b) for(int i = (a); i <= (b); i++) #define FOR(i, n) REP(i, 0, int(n) - 1) #define pii pair < int, int > #define ll long long #define fi first #define se second #define mp make_pair #define vi vector <int> vector<string> v; vector<int> zeros; ll RES = 0; const int MAXL = 18; string tos(int x) { string res = ""; while(x) { res += (x % 10) + '0'; x /= 10; } reverse(res.begin(), res.end()); return res; } string bigger(string a, string b, int i) { int n = a.size(), k = b.size(); zeros[i] = zeros[i-1]; if(n < k) ; else if(n == k && a < b) ; else if(n == k) b += '0'; else if(a.substr(0,k) < b) { FOR(i,n-k) b += '0'; } else if(a.substr(0,k) > b) { FOR(i,n-k) b += '0'; if(b.size() < MAXL) b += '0'; else zeros[i]++; } //puts("*"); else { if(a.size() == MAXL) b = a; else { bool all9 = 1; REP(i,k,n-1) if(a[i] != 9) all9 = 0; if(all9) FOR(i,n-k+1) b += '0'; else { string toadd = ""; bool added = 0; for(int i = n-1; i >= k; i--) { if(a[i] == '9' && added == 0) toadd += '0'; else if (added == 0) { toadd += a[i] + 1; added = 1; } else toadd += a[i]; } reverse(toadd.begin(), toadd.end()); //cout<<"toadd "<<toadd<<"\n"; if(added == 1) b += toadd; else FOR(i,n-k+1) b += '0'; } } } RES += b.size() - k + zeros[i]; return b; } int main() { ios_base::sync_with_stdio(0); int n; cin>>n; FOR(i,n) { int a; cin>>a; v.pb(tos(a)); zeros.pb(0); } /*FOR(i,n) { cout<<v[i]<<"\n"; }*/ REP(i,1,n-1) v[i] = bigger(v[i-1], v[i], i); cout<<RES<<"\n"; return 0; } |