#include<bits/stdc++.h> using namespace std; int a, n, tit; int s[200000]; int tab[18][2000000]; int dzies[12]; long long answ, cl; bool cb, dwa; int sps(int x) { int licz = 0; while(x > 0) { x/=10; licz++; } return licz; } int cyf(int x, int kt) { if(kt == 1) return x%10; int temp = x%dzies[kt]; x%=dzies[kt-1]; temp-=x; temp/=dzies[kt-1]; return temp; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); dzies[1] = 10; for(int i = 2; i<=11; i++) { dzies[i] = dzies[i-1]*10; } cin >> n; for(int i = 1; i<=n; i++) { cin >> a; s[i] = sps(a); for(int j = 1; j<=s[i]; j++) { tab[j][i] = cyf(a, s[i]-j+1); } } cl = s[1]; for(int i = 2; i<=n; i++) { cb = 0; dwa = 0; if(s[i] > cl) { cl = s[i]; continue; } for(int j = 1; j<=s[i]; j++) { if(tab[j][i] > tab[j][i-1]) { cb = 1; answ+=(long long)(cl-(long long)s[i]); break; } if(tab[j][i] < tab[j][i-1]) { cb = 1; answ+=(long long)(cl-(long long)s[i]+1); cl++; break; } } if(cb == 0) { for(int j = s[i]+1; j<=cl; j++) { if(tab[j][i-1] != 9) { cb = 1; break; } } if(cb == 1) { for(long long j = min((long long)17, cl);j >=s[i]+1; j--) { if(tab[j][i-1] !=9 && dwa == 0) { dwa = 1; tab[j][i] = tab[j][i-1]+1; } if(tab[j][i-1] ==9 && dwa == 0) { tab[j][i] = 0; } if(tab[j][i-1] ==9 && dwa == 1) { tab[j][i] = 9; } } answ+=(long long)(cl-(long long)s[i]); } if(cb == 0){ answ+=(long long)(cl-(long long)s[i]+1); cl++; } } } cout << answ; }
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 | #include<bits/stdc++.h> using namespace std; int a, n, tit; int s[200000]; int tab[18][2000000]; int dzies[12]; long long answ, cl; bool cb, dwa; int sps(int x) { int licz = 0; while(x > 0) { x/=10; licz++; } return licz; } int cyf(int x, int kt) { if(kt == 1) return x%10; int temp = x%dzies[kt]; x%=dzies[kt-1]; temp-=x; temp/=dzies[kt-1]; return temp; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); dzies[1] = 10; for(int i = 2; i<=11; i++) { dzies[i] = dzies[i-1]*10; } cin >> n; for(int i = 1; i<=n; i++) { cin >> a; s[i] = sps(a); for(int j = 1; j<=s[i]; j++) { tab[j][i] = cyf(a, s[i]-j+1); } } cl = s[1]; for(int i = 2; i<=n; i++) { cb = 0; dwa = 0; if(s[i] > cl) { cl = s[i]; continue; } for(int j = 1; j<=s[i]; j++) { if(tab[j][i] > tab[j][i-1]) { cb = 1; answ+=(long long)(cl-(long long)s[i]); break; } if(tab[j][i] < tab[j][i-1]) { cb = 1; answ+=(long long)(cl-(long long)s[i]+1); cl++; break; } } if(cb == 0) { for(int j = s[i]+1; j<=cl; j++) { if(tab[j][i-1] != 9) { cb = 1; break; } } if(cb == 1) { for(long long j = min((long long)17, cl);j >=s[i]+1; j--) { if(tab[j][i-1] !=9 && dwa == 0) { dwa = 1; tab[j][i] = tab[j][i-1]+1; } if(tab[j][i-1] ==9 && dwa == 0) { tab[j][i] = 0; } if(tab[j][i-1] ==9 && dwa == 1) { tab[j][i] = 9; } } answ+=(long long)(cl-(long long)s[i]); } if(cb == 0){ answ+=(long long)(cl-(long long)s[i]+1); cl++; } } } cout << answ; } |