Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <cstdlib> #include <iostream> #include <algorithm> using namespace std; unsigned NumDigits(uint32_t x) { return // Num-># Digits->[0-9] 32->bits bs->Binary Search (x >= 100000u // [6-10] [1-5] ? // [6-10] (x >= 10000000u // [8-10] [6-7] ? // [8-10] (x >= 100000000u // [9-10] [8] ? // [9-10] (x >= 1000000000u // [10] [9] ? 10 : 9 ) : 8 ) : // [6-7] (x >= 1000000u // [7] [6] ? 7 : 6 ) ) : // [1-5] (x >= 100u // [3-5] [1-2] ? // [3-5] (x >= 1000u // [4-5] [3] ? // [4-5] (x >= 10000u // [5] [4] ? 5 : 4 ) : 3 ) : // [1-2] (x >= 10u // [2] [1] ? 2 : 1 ) ) ); } bool rowne(int current, int previous, int length, int postfix) { if (current == previous && length == 0 && postfix == 0) return true; int current_length = NumDigits(current); int previous_length = (NumDigits(previous) + length); if (current_length != previous_length) return false; return (previous * powl(10, length) + postfix) == current; } bool wieksza(int current, int previous, int length, int postfix) //ostra wiekszosc { int current_length = NumDigits(current); int previous_length = (NumDigits(previous) + length); if (current_length > previous_length) return true; if (current_length < previous_length) return false; //rowny size, wiec teraz trzeba skonstruowac previous i porownac wartosc //ten kod jest skomplikwoany, ale wykona sie niewiele razy int total_previous = previous * powl(10, length) + postfix; if (current > total_previous) return true; return false; } int normalize(int previous, int prev_length, int prev_postfix, int current) { unsigned int rV = previous; int dlugosc_current = NumDigits(current); int dlugosc_prev = NumDigits(previous); int dlugosc_postfix = NumDigits(prev_postfix); int dlugosc_zer = prev_length - dlugosc_postfix; if (dlugosc_current == dlugosc_prev) return previous; if (dlugosc_current < dlugosc_prev) { //odcinamy ostatnie cyfry z prev return previous/powl(10,dlugosc_prev-dlugosc_current); } //przygotujmy ten postfix int ile_brakuje = dlugosc_current - dlugosc_prev; int ile_zer_dobieramy = min(ile_brakuje, dlugosc_zer); rV = rV * powl(10, ile_brakuje); int ile_liczb_z_postfixa = ile_brakuje - ile_zer_dobieramy; //moze byc ujemne if (ile_liczb_z_postfixa <= 0) { ile_liczb_z_postfixa = 0; return rV; } //ile odciac z posfixa int ile_odciac_z_postfixa = dlugosc_postfix - ile_liczb_z_postfixa; rV += prev_postfix / powl(10, ile_odciac_z_postfixa); return rV; } int main() { unsigned int n,current, previous = 0; unsigned int prev_length = 0, prev_postfix = 0; //dlugos dodanego ciagu, postfix - wartosc tego ciagu , zakladam ze beda tam zera na poczatku long long count = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> current; if (wieksza(current, previous, prev_length, prev_postfix))//biezaca wieksza od poprzedniej, nic nie robimy { count += 0; previous = current; prev_length = 0; prev_postfix = 0; } else { if (rowne(current, previous, prev_length, prev_postfix))//takie same { count += 1; previous = current;prev_length = 1; prev_postfix = 0; } else //current jest mniejsza (wartosciowo) { //A if (NumDigits(current) == NumDigits(previous) + prev_length) //maja te sam� dlugosc { count += 1; previous = current; prev_length = 1; prev_postfix = 0; continue; } //juz wiemy ze current jest mniejsza i jednoczesnie krotsza niz 'previous z dodatkami' //potrzebuje z previous wyciagnac fragment o dlugosci rownej current int normalized_previous = normalize(previous, prev_length, prev_postfix, current); //B1 if (current > normalized_previous) { //dodaje zera tak zeby wyrownac dlugosc, count+=ilosc dodanych zer, previous = curent +'zera' int numCurrent = NumDigits(current); int numPrevious = NumDigits(previous)+prev_length; previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent; count += prev_length; } else { int numCurrent = NumDigits(current); int numPrevious = NumDigits(previous)+prev_length; if (current < normalized_previous) {//B2 previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent + 1; count += prev_length; } else { //B3 current = normalized_previous if (prev_length == 0) //C1 { int incr_prev = previous + 1; int normalized_previous_incr = normalize(incr_prev, 0, 0, current); if (normalized_previous_incr == current) { //previous = > prev_icr, length = 0, postf = 0, count += prev_length - current_length previous = incr_prev; prev_length = 0; prev_postfix = 0; count += NumDigits(previous) - NumDigits(current); } else { //prev = current, postfix = 0; length = numPrevious - numCurrent + 1; count + prev_length previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent + 1; count += prev_length; } } else { //C2 int ile_jest_zer_doklejonych = prev_length - NumDigits(prev_postfix); int incremented_postfix = prev_postfix + 1; if (NumDigits(prev_postfix) == NumDigits(incremented_postfix)) { //D1 //prev = prev, prev_length = length, posftix+=1; count = length = numPrevious - numCurrent; previous = previous; prev_postfix = incremented_postfix; prev_length = prev_length; count += (numPrevious-numCurrent); } else {//D2 if (ile_jest_zer_doklejonych > 0) {//D21 previous = previous; prev_postfix = incremented_postfix; prev_length = prev_length; count += (numPrevious - numCurrent); } else {//D22 //ignorujemy postfix int incr_prev = previous + 1; int normalized_previous_incr = normalize(incr_prev, 0, 0, current); if (normalized_previous_incr == current) //incrementacja poprzedniego jest ok { //previous = > prev_icr, length = 0, postf = 0, count += prev_length - current_length previous = incr_prev; prev_length = prev_length; prev_postfix = 0; count += numPrevious - numCurrent; } else { // inkremnteacja poprzedniego nie dziala, czyli byly pewnie dziewiatki //prev = current, postfix = 0; length = numPrevious - numCurrent + 1; count + prev_length previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent + 1; count += prev_length; } } } } } } } } } cout << count; 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; unsigned NumDigits(uint32_t x) { return // Num-># Digits->[0-9] 32->bits bs->Binary Search (x >= 100000u // [6-10] [1-5] ? // [6-10] (x >= 10000000u // [8-10] [6-7] ? // [8-10] (x >= 100000000u // [9-10] [8] ? // [9-10] (x >= 1000000000u // [10] [9] ? 10 : 9 ) : 8 ) : // [6-7] (x >= 1000000u // [7] [6] ? 7 : 6 ) ) : // [1-5] (x >= 100u // [3-5] [1-2] ? // [3-5] (x >= 1000u // [4-5] [3] ? // [4-5] (x >= 10000u // [5] [4] ? 5 : 4 ) : 3 ) : // [1-2] (x >= 10u // [2] [1] ? 2 : 1 ) ) ); } bool rowne(int current, int previous, int length, int postfix) { if (current == previous && length == 0 && postfix == 0) return true; int current_length = NumDigits(current); int previous_length = (NumDigits(previous) + length); if (current_length != previous_length) return false; return (previous * powl(10, length) + postfix) == current; } bool wieksza(int current, int previous, int length, int postfix) //ostra wiekszosc { int current_length = NumDigits(current); int previous_length = (NumDigits(previous) + length); if (current_length > previous_length) return true; if (current_length < previous_length) return false; //rowny size, wiec teraz trzeba skonstruowac previous i porownac wartosc //ten kod jest skomplikwoany, ale wykona sie niewiele razy int total_previous = previous * powl(10, length) + postfix; if (current > total_previous) return true; return false; } int normalize(int previous, int prev_length, int prev_postfix, int current) { unsigned int rV = previous; int dlugosc_current = NumDigits(current); int dlugosc_prev = NumDigits(previous); int dlugosc_postfix = NumDigits(prev_postfix); int dlugosc_zer = prev_length - dlugosc_postfix; if (dlugosc_current == dlugosc_prev) return previous; if (dlugosc_current < dlugosc_prev) { //odcinamy ostatnie cyfry z prev return previous/powl(10,dlugosc_prev-dlugosc_current); } //przygotujmy ten postfix int ile_brakuje = dlugosc_current - dlugosc_prev; int ile_zer_dobieramy = min(ile_brakuje, dlugosc_zer); rV = rV * powl(10, ile_brakuje); int ile_liczb_z_postfixa = ile_brakuje - ile_zer_dobieramy; //moze byc ujemne if (ile_liczb_z_postfixa <= 0) { ile_liczb_z_postfixa = 0; return rV; } //ile odciac z posfixa int ile_odciac_z_postfixa = dlugosc_postfix - ile_liczb_z_postfixa; rV += prev_postfix / powl(10, ile_odciac_z_postfixa); return rV; } int main() { unsigned int n,current, previous = 0; unsigned int prev_length = 0, prev_postfix = 0; //dlugos dodanego ciagu, postfix - wartosc tego ciagu , zakladam ze beda tam zera na poczatku long long count = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> current; if (wieksza(current, previous, prev_length, prev_postfix))//biezaca wieksza od poprzedniej, nic nie robimy { count += 0; previous = current; prev_length = 0; prev_postfix = 0; } else { if (rowne(current, previous, prev_length, prev_postfix))//takie same { count += 1; previous = current;prev_length = 1; prev_postfix = 0; } else //current jest mniejsza (wartosciowo) { //A if (NumDigits(current) == NumDigits(previous) + prev_length) //maja te sam� dlugosc { count += 1; previous = current; prev_length = 1; prev_postfix = 0; continue; } //juz wiemy ze current jest mniejsza i jednoczesnie krotsza niz 'previous z dodatkami' //potrzebuje z previous wyciagnac fragment o dlugosci rownej current int normalized_previous = normalize(previous, prev_length, prev_postfix, current); //B1 if (current > normalized_previous) { //dodaje zera tak zeby wyrownac dlugosc, count+=ilosc dodanych zer, previous = curent +'zera' int numCurrent = NumDigits(current); int numPrevious = NumDigits(previous)+prev_length; previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent; count += prev_length; } else { int numCurrent = NumDigits(current); int numPrevious = NumDigits(previous)+prev_length; if (current < normalized_previous) {//B2 previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent + 1; count += prev_length; } else { //B3 current = normalized_previous if (prev_length == 0) //C1 { int incr_prev = previous + 1; int normalized_previous_incr = normalize(incr_prev, 0, 0, current); if (normalized_previous_incr == current) { //previous = > prev_icr, length = 0, postf = 0, count += prev_length - current_length previous = incr_prev; prev_length = 0; prev_postfix = 0; count += NumDigits(previous) - NumDigits(current); } else { //prev = current, postfix = 0; length = numPrevious - numCurrent + 1; count + prev_length previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent + 1; count += prev_length; } } else { //C2 int ile_jest_zer_doklejonych = prev_length - NumDigits(prev_postfix); int incremented_postfix = prev_postfix + 1; if (NumDigits(prev_postfix) == NumDigits(incremented_postfix)) { //D1 //prev = prev, prev_length = length, posftix+=1; count = length = numPrevious - numCurrent; previous = previous; prev_postfix = incremented_postfix; prev_length = prev_length; count += (numPrevious-numCurrent); } else {//D2 if (ile_jest_zer_doklejonych > 0) {//D21 previous = previous; prev_postfix = incremented_postfix; prev_length = prev_length; count += (numPrevious - numCurrent); } else {//D22 //ignorujemy postfix int incr_prev = previous + 1; int normalized_previous_incr = normalize(incr_prev, 0, 0, current); if (normalized_previous_incr == current) //incrementacja poprzedniego jest ok { //previous = > prev_icr, length = 0, postf = 0, count += prev_length - current_length previous = incr_prev; prev_length = prev_length; prev_postfix = 0; count += numPrevious - numCurrent; } else { // inkremnteacja poprzedniego nie dziala, czyli byly pewnie dziewiatki //prev = current, postfix = 0; length = numPrevious - numCurrent + 1; count + prev_length previous = current; prev_postfix = 0; prev_length = numPrevious - numCurrent + 1; count += prev_length; } } } } } } } } } cout << count; return 0; } |