#include <bits/stdc++.h> #define dbg if(0) using namespace std; typedef long long ll; ll get_length(ll a){ if(a == 0) return 0; ll len = 1; while(a >= 10){ a /= 10; len++; } return len; } ll get_digit(ll a, ll digit) { ll len = get_length(a); assert(digit <= len); for(ll i = 0; i < len-digit; ++i) a /= 10; return a%10; } ll get_digit_for_compound(ll a, ll current_zeros, ll added, ll i){ ll len_a = get_length(a); //cerr << "a" << a << "len_a: " << len_a << " i: " << i << endl; if(len_a >= i) return get_digit(a, i); ll disposable_zeros = current_zeros - get_length(added); if(i > len_a && i <= len_a + disposable_zeros) return 0; // cerr << "xD hiho" << endl; return get_digit(added, i-len_a-disposable_zeros); } void prll_compound(ll a, ll current_zeros, ll added){ ll pure_zeros = current_zeros - get_length(added); cout << "current zeros: " << current_zeros << " added: " << added << " purezeros:" << pure_zeros << " "; cout << a; for(ll i = 0; i < pure_zeros; ++i) cout << 0; if(added> 0) cout << added; cout << " og: " << a << endl; } ll can_add_padded(ll a, ll current_zeros, ll added, ll current_a) { if(current_zeros > 8){ // sprobuj dodac //cerr << "here!" << endl; ll new_added = added+1; //ll disposable = get_length(a) + current_zeros - get_length(current_a); //cerr << "disposable: " << disposable << " get len: " << get_length(new_added) << endl; //cerr << "new_added: " << new_added << endl; if(get_length(new_added) > current_zeros){ // cerr << "Sad" << endl; return -1; } else return new_added; } else { // sklej i dodaj ll pow = 1ll; for(ll i = 0; i < current_zeros; ++i){ pow *= 10; } ll new_a = a*pow + added + 1; // cerr << "new_a: " << new_a << endl; for(ll i = 1; i <= get_length(current_a); ++i){ if(get_digit(current_a, i) != get_digit(new_a, i)) return -1; } ll total_len = get_length(a) + current_zeros; ll disposable = total_len - get_length(current_a); pow = 1; for(ll i = 0; i < disposable; ++i){ pow *= 10; } new_a = new_a%pow; return new_a; } return -1; } int main() { ios_base::sync_with_stdio(false); ll N; cin >> N; vector<ll> a(N); for(ll i = 0; i < N; ++i){ cin >> a[i]; } ll zeros_counter = 0; ll current_zeros = 0; ll added = 0; ll current_digit = get_digit(a[0], 1); //cerr << "looking at: " << a[0] << " digit is: " << current_digit << endl; ll total_len; ll zeros_to_add; //cout << a[0] << " og: " << a[0] << endl; for(ll i = 1; i < N; ++i) { ll next_digit = get_digit(a[i], 1); total_len = get_length(a[i-1]) + current_zeros; ll new_added = 0; zeros_to_add = 0; dbg cerr << "previous is: " << a[i-1] << " digit is: " << current_digit << " zeros: " << current_zeros << " add: " << added << endl; dbg cerr << "looking at: " << a[i] << " digit is: " << next_digit << endl; if(next_digit < current_digit) { zeros_to_add = total_len - get_length(a[i]) + 1; } else if(next_digit > current_digit) { zeros_to_add = total_len - get_length(a[i]); } else if(next_digit == current_digit) { // first check if a[i] is bigger than previous ll add_more = -1; ll next_len = get_length(a[i]); if(next_len <= total_len) { //prll_compound(a[i-1], current_zeros, added); //cerr << "a[i] " << a[i] << endl; for(ll j = 1; j <= next_len; ++j) { ll next_d = get_digit(a[i], j); ll current_d = get_digit_for_compound(a[i-1], current_zeros, added, j); //cerr << "next_d: " << next_d << " current_d: " << current_d << endl; if(next_d > current_d) { add_more = 0; dbg cerr << "addmore=0" << endl; break; } else if(next_d < current_d) { add_more = 1; dbg cerr << "addmore=1" << endl; break; } } if(add_more == -1) { ll res = can_add_padded(a[i-1], current_zeros, added, a[i]); if(res != -1){ //cerr << "CAN ADD!, res: " << res << endl; add_more = 0; new_added = res; }else{ // cerr << "hiho" << endl; add_more = 1; new_added = 0; } if(current_zeros > 8 && get_length(a[i-1]) > next_len && !add_more) { zeros_counter += (get_length(a[i-1]) - next_len); a[i] = a[i-1]; } } assert(add_more != -1); total_len = (get_length(a[i-1]) + current_zeros); zeros_to_add = total_len - get_length(a[i]) + add_more; } } if(zeros_to_add >= 0){ current_zeros = zeros_to_add; zeros_counter += current_zeros; added = new_added; }else { current_zeros = 0; added = 0; } //cerr << "zeros_counter: " << zeros_counter << " "; //prll_compound(a[i], current_zeros, added); current_digit = next_digit; } cout << zeros_counter << endl; 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | #include <bits/stdc++.h> #define dbg if(0) using namespace std; typedef long long ll; ll get_length(ll a){ if(a == 0) return 0; ll len = 1; while(a >= 10){ a /= 10; len++; } return len; } ll get_digit(ll a, ll digit) { ll len = get_length(a); assert(digit <= len); for(ll i = 0; i < len-digit; ++i) a /= 10; return a%10; } ll get_digit_for_compound(ll a, ll current_zeros, ll added, ll i){ ll len_a = get_length(a); //cerr << "a" << a << "len_a: " << len_a << " i: " << i << endl; if(len_a >= i) return get_digit(a, i); ll disposable_zeros = current_zeros - get_length(added); if(i > len_a && i <= len_a + disposable_zeros) return 0; // cerr << "xD hiho" << endl; return get_digit(added, i-len_a-disposable_zeros); } void prll_compound(ll a, ll current_zeros, ll added){ ll pure_zeros = current_zeros - get_length(added); cout << "current zeros: " << current_zeros << " added: " << added << " purezeros:" << pure_zeros << " "; cout << a; for(ll i = 0; i < pure_zeros; ++i) cout << 0; if(added> 0) cout << added; cout << " og: " << a << endl; } ll can_add_padded(ll a, ll current_zeros, ll added, ll current_a) { if(current_zeros > 8){ // sprobuj dodac //cerr << "here!" << endl; ll new_added = added+1; //ll disposable = get_length(a) + current_zeros - get_length(current_a); //cerr << "disposable: " << disposable << " get len: " << get_length(new_added) << endl; //cerr << "new_added: " << new_added << endl; if(get_length(new_added) > current_zeros){ // cerr << "Sad" << endl; return -1; } else return new_added; } else { // sklej i dodaj ll pow = 1ll; for(ll i = 0; i < current_zeros; ++i){ pow *= 10; } ll new_a = a*pow + added + 1; // cerr << "new_a: " << new_a << endl; for(ll i = 1; i <= get_length(current_a); ++i){ if(get_digit(current_a, i) != get_digit(new_a, i)) return -1; } ll total_len = get_length(a) + current_zeros; ll disposable = total_len - get_length(current_a); pow = 1; for(ll i = 0; i < disposable; ++i){ pow *= 10; } new_a = new_a%pow; return new_a; } return -1; } int main() { ios_base::sync_with_stdio(false); ll N; cin >> N; vector<ll> a(N); for(ll i = 0; i < N; ++i){ cin >> a[i]; } ll zeros_counter = 0; ll current_zeros = 0; ll added = 0; ll current_digit = get_digit(a[0], 1); //cerr << "looking at: " << a[0] << " digit is: " << current_digit << endl; ll total_len; ll zeros_to_add; //cout << a[0] << " og: " << a[0] << endl; for(ll i = 1; i < N; ++i) { ll next_digit = get_digit(a[i], 1); total_len = get_length(a[i-1]) + current_zeros; ll new_added = 0; zeros_to_add = 0; dbg cerr << "previous is: " << a[i-1] << " digit is: " << current_digit << " zeros: " << current_zeros << " add: " << added << endl; dbg cerr << "looking at: " << a[i] << " digit is: " << next_digit << endl; if(next_digit < current_digit) { zeros_to_add = total_len - get_length(a[i]) + 1; } else if(next_digit > current_digit) { zeros_to_add = total_len - get_length(a[i]); } else if(next_digit == current_digit) { // first check if a[i] is bigger than previous ll add_more = -1; ll next_len = get_length(a[i]); if(next_len <= total_len) { //prll_compound(a[i-1], current_zeros, added); //cerr << "a[i] " << a[i] << endl; for(ll j = 1; j <= next_len; ++j) { ll next_d = get_digit(a[i], j); ll current_d = get_digit_for_compound(a[i-1], current_zeros, added, j); //cerr << "next_d: " << next_d << " current_d: " << current_d << endl; if(next_d > current_d) { add_more = 0; dbg cerr << "addmore=0" << endl; break; } else if(next_d < current_d) { add_more = 1; dbg cerr << "addmore=1" << endl; break; } } if(add_more == -1) { ll res = can_add_padded(a[i-1], current_zeros, added, a[i]); if(res != -1){ //cerr << "CAN ADD!, res: " << res << endl; add_more = 0; new_added = res; }else{ // cerr << "hiho" << endl; add_more = 1; new_added = 0; } if(current_zeros > 8 && get_length(a[i-1]) > next_len && !add_more) { zeros_counter += (get_length(a[i-1]) - next_len); a[i] = a[i-1]; } } assert(add_more != -1); total_len = (get_length(a[i-1]) + current_zeros); zeros_to_add = total_len - get_length(a[i]) + add_more; } } if(zeros_to_add >= 0){ current_zeros = zeros_to_add; zeros_counter += current_zeros; added = new_added; }else { current_zeros = 0; added = 0; } //cerr << "zeros_counter: " << zeros_counter << " "; //prll_compound(a[i], current_zeros, added); current_digit = next_digit; } cout << zeros_counter << endl; return 0; } |