#include<bits/stdc++.h> using namespace std; long long tab[200010]; long long pot[20]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n,a,b,d,p,dl,pdl,sdl,s,w,i,j; cin>>n; for(i=1;i<=n;i++) cin>>tab[i]; p=tab[1]; dl=log10(p)+1; pdl=dl; sdl=0; s=0; w=0; for(i=2;i<=n;i++) { d=log10(tab[i])+1; //cout<<d<<"\n"; if(d>dl) { p=tab[i]; s=0; dl=d; pdl=d; sdl=0; continue; } for(j=1;j<=d;j++) { a=(tab[i]/pot[d-j])%10; if(j<=pdl) b=(p/pot[pdl-j])%10; else if(dl-sdl<j) b=(s/pot[dl-j])%10; else b=0; if(a<b) { //cout<<i<<" a<b\n"; p=tab[i]; s=0; dl++; pdl=d; sdl=0; break; } else if(a>b) { //cout<<i<<" a>b\n"; p=tab[i]; s=0; pdl=d; sdl=0; break; } } if(j>d) { if((dl-sdl<j)&&(s%pot[dl-j+1]==pot[dl-j+1]-1)) { //cout<<i<<" jestem na sufiksie, same 9\n"; p=tab[i]; s=0; dl++; pdl=d; sdl=0; } else if((dl==pdl+sdl)&&(s==pot[sdl]-1)) { if(p%pot[pdl-j+1]==pot[pdl-j+1]-1)//reszta z prefiksu to same 9 { //cout<<i<<" jestem na prefiksie, same 9\n"; p=tab[i]; s=0; dl++; pdl=d; sdl=0; } else { //cout<<i<<" jestem na prefiksie, same 9 na sufiksie\n"; p++; s=0; sdl=0; } } else { //cout<<i<<" zwiekszam sufiks o 1\n"; s++; sdl=log10(s)+1; } } w+=dl-d; } cout<<w; 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 102 103 | #include<bits/stdc++.h> using namespace std; long long tab[200010]; long long pot[20]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n,a,b,d,p,dl,pdl,sdl,s,w,i,j; cin>>n; for(i=1;i<=n;i++) cin>>tab[i]; p=tab[1]; dl=log10(p)+1; pdl=dl; sdl=0; s=0; w=0; for(i=2;i<=n;i++) { d=log10(tab[i])+1; //cout<<d<<"\n"; if(d>dl) { p=tab[i]; s=0; dl=d; pdl=d; sdl=0; continue; } for(j=1;j<=d;j++) { a=(tab[i]/pot[d-j])%10; if(j<=pdl) b=(p/pot[pdl-j])%10; else if(dl-sdl<j) b=(s/pot[dl-j])%10; else b=0; if(a<b) { //cout<<i<<" a<b\n"; p=tab[i]; s=0; dl++; pdl=d; sdl=0; break; } else if(a>b) { //cout<<i<<" a>b\n"; p=tab[i]; s=0; pdl=d; sdl=0; break; } } if(j>d) { if((dl-sdl<j)&&(s%pot[dl-j+1]==pot[dl-j+1]-1)) { //cout<<i<<" jestem na sufiksie, same 9\n"; p=tab[i]; s=0; dl++; pdl=d; sdl=0; } else if((dl==pdl+sdl)&&(s==pot[sdl]-1)) { if(p%pot[pdl-j+1]==pot[pdl-j+1]-1)//reszta z prefiksu to same 9 { //cout<<i<<" jestem na prefiksie, same 9\n"; p=tab[i]; s=0; dl++; pdl=d; sdl=0; } else { //cout<<i<<" jestem na prefiksie, same 9 na sufiksie\n"; p++; s=0; sdl=0; } } else { //cout<<i<<" zwiekszam sufiks o 1\n"; s++; sdl=log10(s)+1; } } w+=dl-d; } cout<<w; return 0; } |