#include <bits/stdc++.h> using namespace std; int n, ile_dodac; long long wynik; long long t[200200], x[200200], k[200200], y[200200]; long long pot10[20]; long long dlugosc(int v) { long long res = 0; while(v > 0) { res++; v /= 10; } return res; } long long wyznacz_k(int a, int b) { for(int m = 9; m > 0; m--) { if(k[b]-m < 0) continue; long long aa = t[a]; long long reszta = 0; long long bb = x[b]; reszta = aa%pot10[m]; aa /= pot10[m]; if(aa > bb) return (k[b]-m); else if(aa == bb && reszta > 0) return (k[b]-m); } for(int m = 0; m <= 9; m++) { long long aa = t[a]; long long bb = x[b]; bb /= pot10[m]; if(aa > bb) return (k[b]+m); } return 1e9; } bool jest_prefiksem(int a, int b) { int aa = t[a]; int bb = x[b]; int yy = y[b]; vector<int> va; vector<int> vb; vector<int> vy; while(aa > 0) { va.push_back(aa%10); aa /= 10; } while(bb > 0) { vb.push_back(bb%10); bb /= 10; } while(yy > 0) { vy.push_back(yy%10); yy /= 10; } reverse(va.begin(), va.end()); reverse(vb.begin(), vb.end()); //vy nie odwracamy for(int i = 0; i < min(va.size(), vb.size()); i++) { if(va[i] != vb[i]) return false; } int pot = k[b]-1; for(int i = vb.size(); i < va.size(); i++) { if(pot < vy.size() && va[i] != vy[pot]) return false; else if(va[i] != 0) return false; pot--; } return true; } int main() { cin>>n; pot10[0] = 1; for(int i = 1; i <= 15; i++) { pot10[i] = pot10[i-1]*10LL; } for(int i = 1; i <= n; i++) { cin>>t[i]; } x[1] = t[1]; for(int i = 2; i <= n; i++) { ile_dodac = 0; bool ok = false; if(jest_prefiksem(i, i-1) && dlugosc(t[i]) != dlugosc(x[i-1]) + k[i-1]) { // cout<<i<<" :)"<<endl; if(k[i-1] <= 10 && (y[i-1]+1) >= pot10[k[i-1]]) { int px = x[i-1], py = y[i-1]; x[i-1]++; y[i-1] = 0; //if(i == 2) cout<<"!"<<jest_prefiksem(i, i-1)<<" "<<dlugosc(t[i])<<" "<<dlugosc(x[i-1]) + k[i-1]<<endl; if(jest_prefiksem(i, i-1) && dlugosc(t[i]) != dlugosc(x[i-1]) + k[i-1]) { x[i] = x[i-1]; k[i] = k[i-1]; y[i] = 0; wynik += dlugosc(x[i]) + k[i] - dlugosc(t[i]); ok = true; } x[i-1] = px; y[i-1] = py; } else { x[i] = x[i-1]; k[i] = k[i-1]; y[i] = y[i-1]+1; wynik += dlugosc(x[i]) + k[i] - dlugosc(t[i]); ok = true; } } if(ok == false) { x[i] = t[i]; k[i] = wyznacz_k(i, i-1); y[i] = 0; wynik += k[i]; } //cout<<i<<" "<<x[i]<<" "<<k[i]<<" "<<y[i]<<endl; ok = false; } cout<<wynik<<endl; }
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 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <bits/stdc++.h> using namespace std; int n, ile_dodac; long long wynik; long long t[200200], x[200200], k[200200], y[200200]; long long pot10[20]; long long dlugosc(int v) { long long res = 0; while(v > 0) { res++; v /= 10; } return res; } long long wyznacz_k(int a, int b) { for(int m = 9; m > 0; m--) { if(k[b]-m < 0) continue; long long aa = t[a]; long long reszta = 0; long long bb = x[b]; reszta = aa%pot10[m]; aa /= pot10[m]; if(aa > bb) return (k[b]-m); else if(aa == bb && reszta > 0) return (k[b]-m); } for(int m = 0; m <= 9; m++) { long long aa = t[a]; long long bb = x[b]; bb /= pot10[m]; if(aa > bb) return (k[b]+m); } return 1e9; } bool jest_prefiksem(int a, int b) { int aa = t[a]; int bb = x[b]; int yy = y[b]; vector<int> va; vector<int> vb; vector<int> vy; while(aa > 0) { va.push_back(aa%10); aa /= 10; } while(bb > 0) { vb.push_back(bb%10); bb /= 10; } while(yy > 0) { vy.push_back(yy%10); yy /= 10; } reverse(va.begin(), va.end()); reverse(vb.begin(), vb.end()); //vy nie odwracamy for(int i = 0; i < min(va.size(), vb.size()); i++) { if(va[i] != vb[i]) return false; } int pot = k[b]-1; for(int i = vb.size(); i < va.size(); i++) { if(pot < vy.size() && va[i] != vy[pot]) return false; else if(va[i] != 0) return false; pot--; } return true; } int main() { cin>>n; pot10[0] = 1; for(int i = 1; i <= 15; i++) { pot10[i] = pot10[i-1]*10LL; } for(int i = 1; i <= n; i++) { cin>>t[i]; } x[1] = t[1]; for(int i = 2; i <= n; i++) { ile_dodac = 0; bool ok = false; if(jest_prefiksem(i, i-1) && dlugosc(t[i]) != dlugosc(x[i-1]) + k[i-1]) { // cout<<i<<" :)"<<endl; if(k[i-1] <= 10 && (y[i-1]+1) >= pot10[k[i-1]]) { int px = x[i-1], py = y[i-1]; x[i-1]++; y[i-1] = 0; //if(i == 2) cout<<"!"<<jest_prefiksem(i, i-1)<<" "<<dlugosc(t[i])<<" "<<dlugosc(x[i-1]) + k[i-1]<<endl; if(jest_prefiksem(i, i-1) && dlugosc(t[i]) != dlugosc(x[i-1]) + k[i-1]) { x[i] = x[i-1]; k[i] = k[i-1]; y[i] = 0; wynik += dlugosc(x[i]) + k[i] - dlugosc(t[i]); ok = true; } x[i-1] = px; y[i-1] = py; } else { x[i] = x[i-1]; k[i] = k[i-1]; y[i] = y[i-1]+1; wynik += dlugosc(x[i]) + k[i] - dlugosc(t[i]); ok = true; } } if(ok == false) { x[i] = t[i]; k[i] = wyznacz_k(i, i-1); y[i] = 0; wynik += k[i]; } //cout<<i<<" "<<x[i]<<" "<<k[i]<<" "<<y[i]<<endl; ok = false; } cout<<wynik<<endl; } |