#include<bits/stdc++.h> using namespace std; #ifndef d #define d(...) #endif #define st first #define nd second #define pb push_back #define siz(c) (int)(c).size() #define all(c) (c).begin(), (c).end() typedef long long LL; typedef long double LD; constexpr int INF=1e9+7; constexpr LL INFL=1e18; template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } constexpr int maxn=200005,C=20; int n; LL res=0; void dodaj(string& s) { if(siz(s)>C) return; reverse(all(s)); int carr=1; for(int i=0;i<siz(s) and carr!=0;i++) { if(s[i]=='9') s[i]='0'; else { s[i]+=1; carr=0; } } if(carr) s+='1'; reverse(all(s)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; string lst="0"; int dl=1; for(int i=0;i<n;i++) { string t; cin>>t; if(siz(t)>dl) { lst=t; dl=siz(t); continue; } int pt=0; res+=dl-siz(t); assert(siz(t)<=siz(lst)); for(int& pos=pt;pos<siz(t);pos++) { if(t[pos]<lst[pos]) { dl++, res++; lst=t; break; } if(t[pos]>lst[pos]) { lst=t; break; } } if(pt!=siz(t)) lst+=string(min(C,dl-siz(t)),'0'); else { bool flag=false; for(int j=pt;j<siz(lst);j++) { if(lst[j]!='9') { flag=true; break; } } if(flag) dodaj(lst); else { dl++,res++; lst=t; lst+=string(min(C,dl-siz(t)),'0'); } } } 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 | #include<bits/stdc++.h> using namespace std; #ifndef d #define d(...) #endif #define st first #define nd second #define pb push_back #define siz(c) (int)(c).size() #define all(c) (c).begin(), (c).end() typedef long long LL; typedef long double LD; constexpr int INF=1e9+7; constexpr LL INFL=1e18; template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } constexpr int maxn=200005,C=20; int n; LL res=0; void dodaj(string& s) { if(siz(s)>C) return; reverse(all(s)); int carr=1; for(int i=0;i<siz(s) and carr!=0;i++) { if(s[i]=='9') s[i]='0'; else { s[i]+=1; carr=0; } } if(carr) s+='1'; reverse(all(s)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; string lst="0"; int dl=1; for(int i=0;i<n;i++) { string t; cin>>t; if(siz(t)>dl) { lst=t; dl=siz(t); continue; } int pt=0; res+=dl-siz(t); assert(siz(t)<=siz(lst)); for(int& pos=pt;pos<siz(t);pos++) { if(t[pos]<lst[pos]) { dl++, res++; lst=t; break; } if(t[pos]>lst[pos]) { lst=t; break; } } if(pt!=siz(t)) lst+=string(min(C,dl-siz(t)),'0'); else { bool flag=false; for(int j=pt;j<siz(lst);j++) { if(lst[j]!='9') { flag=true; break; } } if(flag) dodaj(lst); else { dl++,res++; lst=t; lst+=string(min(C,dl-siz(t)),'0'); } } } cout<<res<<"\n"; return 0; } |