#include<bits/stdc++.h> #define SZ(a) (int)a.size() #define FOR(i,N) for(int i=0;i<N;i++) #define For(i,a) FOR(i,SZ(a)) #define mp make_pair #define sd second #define ft first typedef long long ll; using namespace std; int nrdg(ll n){ int lg=0; while(n>0){ lg++; n/=10; } return lg; } ll POW[20]; struct nr{ ll bgn=0; ll end=0; int pw=0; void norm(){ if(nrdg(end)>pw){ bgn*=POW[pw]; pw=0; bgn+=end; end=0; } while(bgn%10==0){ pw++; bgn/=10; } } void inc(){ end++; norm(); } int lght(){ return nrdg(bgn)+pw; } }; inline ll getfront(nr last, nr a){ ll f; if(a.pw>last.pw){ f=last.bgn/POW[a.pw-last.pw]; }else{ f=last.bgn*POW[last.pw-a.pw]; if(nrdg(last.end)>a.pw){ f+=last.end/POW[a.pw]; } } return f; } int main(){ POW[0]=1; for(int i=1;i<20;i++){ POW[i]=POW[i-1]*10LL; } int N; nr last; scanf("%d",&N); ll sm=0; scanf("%lld",&last.bgn); for(int i=1;i<N;i++){ nr a; scanf("%lld",&a.bgn); if(a.lght()>last.lght()){ last=a; } else{ sm+=last.lght()-a.lght(); a.pw+=last.lght()-a.lght(); ll f; f=getfront(last,a); //printf(" %lld %lld\n",f,a.bgn); if(a.bgn>f){ last=a; }else if(a.bgn==f){ last.inc(); f=getfront(last,a); if(a.bgn!=f){ a.pw++; sm++; last=a; } }else{ sm++; a.pw++; last=a; } } last.norm(); //printf("%lld : %lld*10^%d + %lld\n",sm,last.bgn,last.pw,last.end); } printf("%lld\n",sm); }
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 102 | #include<bits/stdc++.h> #define SZ(a) (int)a.size() #define FOR(i,N) for(int i=0;i<N;i++) #define For(i,a) FOR(i,SZ(a)) #define mp make_pair #define sd second #define ft first typedef long long ll; using namespace std; int nrdg(ll n){ int lg=0; while(n>0){ lg++; n/=10; } return lg; } ll POW[20]; struct nr{ ll bgn=0; ll end=0; int pw=0; void norm(){ if(nrdg(end)>pw){ bgn*=POW[pw]; pw=0; bgn+=end; end=0; } while(bgn%10==0){ pw++; bgn/=10; } } void inc(){ end++; norm(); } int lght(){ return nrdg(bgn)+pw; } }; inline ll getfront(nr last, nr a){ ll f; if(a.pw>last.pw){ f=last.bgn/POW[a.pw-last.pw]; }else{ f=last.bgn*POW[last.pw-a.pw]; if(nrdg(last.end)>a.pw){ f+=last.end/POW[a.pw]; } } return f; } int main(){ POW[0]=1; for(int i=1;i<20;i++){ POW[i]=POW[i-1]*10LL; } int N; nr last; scanf("%d",&N); ll sm=0; scanf("%lld",&last.bgn); for(int i=1;i<N;i++){ nr a; scanf("%lld",&a.bgn); if(a.lght()>last.lght()){ last=a; } else{ sm+=last.lght()-a.lght(); a.pw+=last.lght()-a.lght(); ll f; f=getfront(last,a); //printf(" %lld %lld\n",f,a.bgn); if(a.bgn>f){ last=a; }else if(a.bgn==f){ last.inc(); f=getfront(last,a); if(a.bgn!=f){ a.pw++; sm++; last=a; } }else{ sm++; a.pw++; last=a; } } last.norm(); //printf("%lld : %lld*10^%d + %lld\n",sm,last.bgn,last.pw,last.end); } printf("%lld\n",sm); } |