#include <bits/stdc++.h> using namespace std; const long long MAX = 1223372036854775804; long long cnt = 0; int n = 0; long long act, ex; bool deb = 0; int getExp(int num){ long long pow10 = 1; long long a = 0; while(pow10 <= num){ pow10 *= 10; a++; } if(a > 0) a--; return a; } //został przypadek gdy b jest prefiksem a i nie możemy dodać do niego więcej //literek int checkIf(long long a, long long b){ //sprawdza czy b jest prefiksem a long long m = 1; int pos = 0; while(a/m != b && a > 0 && m < (long long)(MAX)){ m *= 10; pos++; } if(a/m == b) return pos; return -1; } void sol(){ cin >> act; ex = 0; for(int a = 2; a <= n; a++){ int num; cin >> num; int wyk1 = getExp(act); int wyk2 = getExp(num); int ex2 = 0; if(wyk1 + ex >= wyk2){ ex2 = (wyk1 + ex) - wyk2; int exTemp; long long temp1 = act; long long temp2 = num; if(wyk1 > wyk2){ exTemp = wyk1-wyk2; temp2 = num*(int)pow(10,exTemp); } else{ exTemp = wyk2-wyk1; temp1 = act*(int)pow(10,exTemp); } if(deb){ cout << "act: " << act << " ex: " << ex << " num: " << num << " ex2: " << ex2 << endl; cout << temp1 << " " << temp2 << endl; } if(temp1 >= temp2){ int checkPos = checkIf(act,num); int revCheckPos = checkIf(num,act); if(deb) cout << "checkPos: " << checkPos << endl; long long z = 0; if(checkPos != -1 || revCheckPos != -1){ z = temp1 - temp2; if(deb){ cout << "temp1/2: " << temp1 << " " << temp2 << endl; cout << "z : " << z << endl; } if(ex2 < 10 && (z+1) < (long long)pow(10,ex2)) z++; else{ z = 0; ex2++; } int mn = getExp(z); if(deb) cout << "mn: " << mn << endl; num *= (long long)pow(10,(mn+1)); num += z; ex2 -= (mn+1); cnt += (mn+1); } else{ ex2++; } } cnt += ex2; } act = num; ex = ex2; } cout << cnt << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; sol(); }
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 | #include <bits/stdc++.h> using namespace std; const long long MAX = 1223372036854775804; long long cnt = 0; int n = 0; long long act, ex; bool deb = 0; int getExp(int num){ long long pow10 = 1; long long a = 0; while(pow10 <= num){ pow10 *= 10; a++; } if(a > 0) a--; return a; } //został przypadek gdy b jest prefiksem a i nie możemy dodać do niego więcej //literek int checkIf(long long a, long long b){ //sprawdza czy b jest prefiksem a long long m = 1; int pos = 0; while(a/m != b && a > 0 && m < (long long)(MAX)){ m *= 10; pos++; } if(a/m == b) return pos; return -1; } void sol(){ cin >> act; ex = 0; for(int a = 2; a <= n; a++){ int num; cin >> num; int wyk1 = getExp(act); int wyk2 = getExp(num); int ex2 = 0; if(wyk1 + ex >= wyk2){ ex2 = (wyk1 + ex) - wyk2; int exTemp; long long temp1 = act; long long temp2 = num; if(wyk1 > wyk2){ exTemp = wyk1-wyk2; temp2 = num*(int)pow(10,exTemp); } else{ exTemp = wyk2-wyk1; temp1 = act*(int)pow(10,exTemp); } if(deb){ cout << "act: " << act << " ex: " << ex << " num: " << num << " ex2: " << ex2 << endl; cout << temp1 << " " << temp2 << endl; } if(temp1 >= temp2){ int checkPos = checkIf(act,num); int revCheckPos = checkIf(num,act); if(deb) cout << "checkPos: " << checkPos << endl; long long z = 0; if(checkPos != -1 || revCheckPos != -1){ z = temp1 - temp2; if(deb){ cout << "temp1/2: " << temp1 << " " << temp2 << endl; cout << "z : " << z << endl; } if(ex2 < 10 && (z+1) < (long long)pow(10,ex2)) z++; else{ z = 0; ex2++; } int mn = getExp(z); if(deb) cout << "mn: " << mn << endl; num *= (long long)pow(10,(mn+1)); num += z; ex2 -= (mn+1); cnt += (mn+1); } else{ ex2++; } } cnt += ex2; } act = num; ex = ex2; } cout << cnt << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; sol(); } |