#include <bits/stdc++.h> #include <unordered_set> using namespace std; #define st first #define nd second #define pb push_back #define mp make_pair #define klar(v) memset(v, 0, sizeof(v)) #define bust ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define gcd(a, b) __gcd(a, b); #define debug(x) cout << #x << " " << x << endl; #define endl "\n" typedef vector<int> vi; typedef vector<pair<int, int> > vpii; typedef vector<long long> vll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef long long ll; const int maxn = 5e5+100; ll arr[maxn]; int dll(ll x){ int ret = 0; while(x > 0){ ret++; x /= 10; } return ret; } ll mlicz(ll pref, ll dr, ll rr){ ll ret = 0; ret += pref; for(int i = 0; i < dr; i++) ret *= 10; ret += rr; return ret; } ll wyc(ll l, int dl){ ll h = dll(l);; while(h-dl > 0){ l /= 10; h--; } return l; } void wyplicz(ll pref, ll dr, ll rr){ cout << "liczba: "; // cout << pref << " " << dr << " " << rr << endl; string wyp; // wyp += to_string(pref); cout << pref; int k = dr-dll(rr); while(k > 0){ cout << 0; k--; } if(rr != 0)cout << rr; cout << endl; } int main(){ bust int n; cin >> n; for(int i = 1; i <= n; i++) cin >> arr[i]; ll pref = arr[1], dr = 0, rr = 0; // cout << "zero if" << endl; // cout << pref << endl; ll ans = 0; for(int i = 2; i <= n; i++){ int dla = dll(arr[i]), dlb = dll(pref); if(dla >= dlb+dr){ // cout << "pierwszy if" << endl; while(dr > 0){ pref *= 10; dr--; } pref += rr; rr = 0; // debug(pref); if(pref < arr[i]) pref = arr[i]; else if(pref > arr[i]){ while(arr[i] < pref){ arr[i] *= 10; ans++; } // cout << arr[i] << " " << pref << endl; if(arr[i] == pref)arr[i]++; pref = arr[i]; } else{ ans++; dr++; } } else{ // cout << "drugi if" << endl; if(dlb+dr < 16){ while(dr > 0){ pref *= 10; dr--; } pref += rr; rr = 0; } else{ while(pref < 1e9 and dr > 0){ pref *= 10; dr--; } } // cout << "params: " << pref << " " << dr << " " << rr << endl; ll l = wyc(pref, dla); if(l < arr[i]){ dr += (dll(pref)-dla); pref = arr[i]; rr = 0; } else if(l == arr[i]){ // cout << "zagniezdzony drugi" << endl; if(dr == 0){ pref++; l = wyc(pref, dla); // cout << "l and pref\n" << l << " " << pref << endl; if(l != arr[i]){ dr = dll(pref)-dll(arr[i]); if(dll(pref) == dll(pref-1))dr++; pref = arr[i]; } } else{ rr++; } } else{ dr += (dll(pref)-dla)+1; pref = arr[i]; } ans += dll(pref)+dr-dll(arr[i]); // cout << "params przy wyjsciu: " << l << " " << pref << " " << dr << " " << rr << endl; } // wyplicz(pref, dr, rr); // debug(ans); } cout << ans << 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 144 145 146 147 148 149 150 151 152 153 154 | #include <bits/stdc++.h> #include <unordered_set> using namespace std; #define st first #define nd second #define pb push_back #define mp make_pair #define klar(v) memset(v, 0, sizeof(v)) #define bust ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define gcd(a, b) __gcd(a, b); #define debug(x) cout << #x << " " << x << endl; #define endl "\n" typedef vector<int> vi; typedef vector<pair<int, int> > vpii; typedef vector<long long> vll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef long long ll; const int maxn = 5e5+100; ll arr[maxn]; int dll(ll x){ int ret = 0; while(x > 0){ ret++; x /= 10; } return ret; } ll mlicz(ll pref, ll dr, ll rr){ ll ret = 0; ret += pref; for(int i = 0; i < dr; i++) ret *= 10; ret += rr; return ret; } ll wyc(ll l, int dl){ ll h = dll(l);; while(h-dl > 0){ l /= 10; h--; } return l; } void wyplicz(ll pref, ll dr, ll rr){ cout << "liczba: "; // cout << pref << " " << dr << " " << rr << endl; string wyp; // wyp += to_string(pref); cout << pref; int k = dr-dll(rr); while(k > 0){ cout << 0; k--; } if(rr != 0)cout << rr; cout << endl; } int main(){ bust int n; cin >> n; for(int i = 1; i <= n; i++) cin >> arr[i]; ll pref = arr[1], dr = 0, rr = 0; // cout << "zero if" << endl; // cout << pref << endl; ll ans = 0; for(int i = 2; i <= n; i++){ int dla = dll(arr[i]), dlb = dll(pref); if(dla >= dlb+dr){ // cout << "pierwszy if" << endl; while(dr > 0){ pref *= 10; dr--; } pref += rr; rr = 0; // debug(pref); if(pref < arr[i]) pref = arr[i]; else if(pref > arr[i]){ while(arr[i] < pref){ arr[i] *= 10; ans++; } // cout << arr[i] << " " << pref << endl; if(arr[i] == pref)arr[i]++; pref = arr[i]; } else{ ans++; dr++; } } else{ // cout << "drugi if" << endl; if(dlb+dr < 16){ while(dr > 0){ pref *= 10; dr--; } pref += rr; rr = 0; } else{ while(pref < 1e9 and dr > 0){ pref *= 10; dr--; } } // cout << "params: " << pref << " " << dr << " " << rr << endl; ll l = wyc(pref, dla); if(l < arr[i]){ dr += (dll(pref)-dla); pref = arr[i]; rr = 0; } else if(l == arr[i]){ // cout << "zagniezdzony drugi" << endl; if(dr == 0){ pref++; l = wyc(pref, dla); // cout << "l and pref\n" << l << " " << pref << endl; if(l != arr[i]){ dr = dll(pref)-dll(arr[i]); if(dll(pref) == dll(pref-1))dr++; pref = arr[i]; } } else{ rr++; } } else{ dr += (dll(pref)-dla)+1; pref = arr[i]; } ans += dll(pref)+dr-dll(arr[i]); // cout << "params przy wyjsciu: " << l << " " << pref << " " << dr << " " << rr << endl; } // wyplicz(pref, dr, rr); // debug(ans); } cout << ans << endl; } |