#include <bits/stdc++.h> #define maxn 1000009 using namespace std; long long n; long long tab[maxn]; long long tab2[maxn][11]; long long tstab[maxn]; long long wyn; long long tstab2[maxn]; long long pot[11]; bool czy2=false; void pot10() { pot[0]=1; for(int i=1; i<=10; i++) { pot[i]=pot[i-1]*10; } } bool czy(int x, int y, int l, int m) { while(l>=0 && m>=0) { int pom1,pom2; if(l>=0) pom1=tab2[x][l]; else pom1=tab2[x][0]; if(m>=0) pom2=tab2[y][m]; else pom2=tab2[y][0]; if(pom1>pom2) return true; else if(pom2>pom1) return false; l--; m--; } czy2=true; return false; } int main() { scanf("%d" , &n); pot10(); int rowne=0; for(int i=0; i<n; i++) { scanf("%d", &tab[i]); long long b=tab[i]; long long ts; if(b<10) ts=0; else if(b<100) ts=1; else if(b<1000) ts=2; else if(b<10000) ts=3; else if(b<100000) ts=4; else if(b<1000000) ts=5; else if(b<10000000) ts=6; else if(b<100000000) ts=7; else if(b<1000000000) ts=8; else ts=9; tstab[i]=ts; tstab2[i]=ts; while(ts>=0) { tab2[i][ts]=(b%pot[ts+1])/pot[ts]; ts--; } if(i) { if(czy(i-1, i, tstab[i-1], tstab[i])) { tstab2[i]=max(tstab2[i],tstab2[i-1]+1); wyn+=(tstab2[i]-tstab[i]); rowne=0; } else { int xd = tstab2[i]; tstab2[i]=max(tstab2[i], tstab2[i-1]); wyn+=(tstab2[i]-tstab[i]); if(rowne!=0 && czy2 && rowne%10==0) { tstab2[i]++; wyn++; rowne=0; } else if(czy2 && xd==tstab2[i-1]) { tstab2[i]++; wyn++; rowne=0; } else if(czy2 && rowne==0) { rowne=2; } else if(czy2) { rowne++; } else { rowne=0; } } //puts("---------------------"); //cout<<tstab2[i]<<" "<<wyn<<endl; //puts("-----------------------"); } } cout<<wyn; 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #include <bits/stdc++.h> #define maxn 1000009 using namespace std; long long n; long long tab[maxn]; long long tab2[maxn][11]; long long tstab[maxn]; long long wyn; long long tstab2[maxn]; long long pot[11]; bool czy2=false; void pot10() { pot[0]=1; for(int i=1; i<=10; i++) { pot[i]=pot[i-1]*10; } } bool czy(int x, int y, int l, int m) { while(l>=0 && m>=0) { int pom1,pom2; if(l>=0) pom1=tab2[x][l]; else pom1=tab2[x][0]; if(m>=0) pom2=tab2[y][m]; else pom2=tab2[y][0]; if(pom1>pom2) return true; else if(pom2>pom1) return false; l--; m--; } czy2=true; return false; } int main() { scanf("%d" , &n); pot10(); int rowne=0; for(int i=0; i<n; i++) { scanf("%d", &tab[i]); long long b=tab[i]; long long ts; if(b<10) ts=0; else if(b<100) ts=1; else if(b<1000) ts=2; else if(b<10000) ts=3; else if(b<100000) ts=4; else if(b<1000000) ts=5; else if(b<10000000) ts=6; else if(b<100000000) ts=7; else if(b<1000000000) ts=8; else ts=9; tstab[i]=ts; tstab2[i]=ts; while(ts>=0) { tab2[i][ts]=(b%pot[ts+1])/pot[ts]; ts--; } if(i) { if(czy(i-1, i, tstab[i-1], tstab[i])) { tstab2[i]=max(tstab2[i],tstab2[i-1]+1); wyn+=(tstab2[i]-tstab[i]); rowne=0; } else { int xd = tstab2[i]; tstab2[i]=max(tstab2[i], tstab2[i-1]); wyn+=(tstab2[i]-tstab[i]); if(rowne!=0 && czy2 && rowne%10==0) { tstab2[i]++; wyn++; rowne=0; } else if(czy2 && xd==tstab2[i-1]) { tstab2[i]++; wyn++; rowne=0; } else if(czy2 && rowne==0) { rowne=2; } else if(czy2) { rowne++; } else { rowne=0; } } //puts("---------------------"); //cout<<tstab2[i]<<" "<<wyn<<endl; //puts("-----------------------"); } } cout<<wyn; return 0; } |