#include <iostream> #include <vector> #include <cmath> #include <string> #include <cstdio> #include <cstdlib> using namespace std; string dodaj(string x){ int i = x.size()-1; if(x[i]=='9'){ x[i]='0'; i--; for(; x[i]=='9';i--){ x[i] = '0'; if(i==0){ x+='0'; break; } } } x[i]++; return x; } bool comp(string a, string b){ if(a.size()>b.size()) return 1; if(b.size()>a.size()) return 0; for(int i=0;i<a.size();i++){ if(a[i]>b[i]) return 1; if(b[i]>a[i]) return 0; } return 0; } int main() { int n; cin >> n; vector<string> a; string p; cin>>p; a.push_back(p); long long res=0; for(int i=1; i<n; i++){ string x; cin>>x; if(comp(x, a[i-1])) a.push_back(x); else{ string prev = dodaj(a[i-1]); if(prev.substr(0,x.size()) == x){ a.push_back(prev); res+=(prev.size()-x.size()); } else{ long long licz=0; while(!comp(x,prev)){ x+='0'; licz++; } a.push_back(x); res+=licz; } } } 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 | #include <iostream> #include <vector> #include <cmath> #include <string> #include <cstdio> #include <cstdlib> using namespace std; string dodaj(string x){ int i = x.size()-1; if(x[i]=='9'){ x[i]='0'; i--; for(; x[i]=='9';i--){ x[i] = '0'; if(i==0){ x+='0'; break; } } } x[i]++; return x; } bool comp(string a, string b){ if(a.size()>b.size()) return 1; if(b.size()>a.size()) return 0; for(int i=0;i<a.size();i++){ if(a[i]>b[i]) return 1; if(b[i]>a[i]) return 0; } return 0; } int main() { int n; cin >> n; vector<string> a; string p; cin>>p; a.push_back(p); long long res=0; for(int i=1; i<n; i++){ string x; cin>>x; if(comp(x, a[i-1])) a.push_back(x); else{ string prev = dodaj(a[i-1]); if(prev.substr(0,x.size()) == x){ a.push_back(prev); res+=(prev.size()-x.size()); } else{ long long licz=0; while(!comp(x,prev)){ x+='0'; licz++; } a.push_back(x); res+=licz; } } } cout << res; return 0; } |