#include <bits/stdc++.h> using namespace std; typedef long long LL; LL pot[15]; int ostd(string &s) //ostatnie 9 { int ind = s.size() - 1; while(ind > 0 && s[ind] == '9') ind--; return ind; } int pierw(string &s1, string &s2) { int ub = min(s1.size(), s2.size()); int ind = 0; while(ind < ub && s1[ind] == s2[ind]) ind++; return ind; } bool Prefix(string &s1, string &s2, int ind) { if(ind < s1.size() && ind < s2.size()) return 0; return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); pot[0] = 1; for(int i = 1; i < 15; i++) pot[i] = pot[i - 1] * 10; int n; cin >> n; string tmp[2]; LL w = 0; LL ost = 0; string sost; int il = 0; string tmpv2; for(int i = 0; i < n; i++) { string poprzedni, teraz; int p; bool co; bool flag = 0; LL akt; cin >> akt; tmp[i % 2] = to_string(akt); if(i == 0) tmpv2 = tmp[0]; else { poprzedni = tmpv2; teraz = tmp[i%2]; p = pierw(poprzedni, teraz); co = Prefix(poprzedni, teraz, p); if((!co) || (co && tmp[i%2].size() > tmpv2.size())) flag = 1; } if(sost.size() <= 14){ if(akt <= ost) { string sakt = to_string(akt); int p = pierw(sakt, sost); int d = ostd(sost); int roz = sost.size() - sakt.size(); if(p != sakt.size()) { // cout << akt <<" " << ost << " " <<"dupa" << p << " " << d << " " << roz << endl; if(sakt[p] > sost[p]) { ost = akt * pot[roz]; // cout << "kupa" << " " << akt << " "<< pot[roz] << endl; w += roz; } else if(sakt[p] < sost[p]) { ost = akt * pot[roz + 1]; w += roz + 1; } } else { // cout << "FLAG" << " " << p << " " << d << " " << sost[p] << " " << sakt[p] << " " << sost.size() << " " << sakt.size() << endl; if(d < p) { ost = akt * pot[roz + 1]; //cout << akt << " " << pot[roz + 1] << endl; w+= roz + 1; } else { LL nowa = 0; int it = 0; //cout << "KUPCIA" << sost << endl; while(it != d) { nowa = (nowa + sost[it] - '0') * 10; it++; } //cout << nowa << endl; nowa = (nowa + sost[it] - '0' + 1); nowa *= pot[sost.size() - it - 1]; ost = nowa; w += roz; } } //cout << sost << endl; } else ost = akt; sost = to_string(ost); il=sost.size(); } else { //cout << "FAJER" << " " <<co << " " << p << " " <<teraz << " " << poprzedni << " " << teraz[p] << " " << poprzedni[p] << endl; if(co == 0) { if(teraz[p] > tmpv2[p]) w += il - teraz.size(); else { il++; w += il - teraz.size(); } } else w += il - teraz.size(); } //cout << w << " " << il << " " << teraz << " " << tmpv2 << endl; if(flag) tmpv2 = teraz; } cout << w; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; LL pot[15]; int ostd(string &s) //ostatnie 9 { int ind = s.size() - 1; while(ind > 0 && s[ind] == '9') ind--; return ind; } int pierw(string &s1, string &s2) { int ub = min(s1.size(), s2.size()); int ind = 0; while(ind < ub && s1[ind] == s2[ind]) ind++; return ind; } bool Prefix(string &s1, string &s2, int ind) { if(ind < s1.size() && ind < s2.size()) return 0; return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); pot[0] = 1; for(int i = 1; i < 15; i++) pot[i] = pot[i - 1] * 10; int n; cin >> n; string tmp[2]; LL w = 0; LL ost = 0; string sost; int il = 0; string tmpv2; for(int i = 0; i < n; i++) { string poprzedni, teraz; int p; bool co; bool flag = 0; LL akt; cin >> akt; tmp[i % 2] = to_string(akt); if(i == 0) tmpv2 = tmp[0]; else { poprzedni = tmpv2; teraz = tmp[i%2]; p = pierw(poprzedni, teraz); co = Prefix(poprzedni, teraz, p); if((!co) || (co && tmp[i%2].size() > tmpv2.size())) flag = 1; } if(sost.size() <= 14){ if(akt <= ost) { string sakt = to_string(akt); int p = pierw(sakt, sost); int d = ostd(sost); int roz = sost.size() - sakt.size(); if(p != sakt.size()) { // cout << akt <<" " << ost << " " <<"dupa" << p << " " << d << " " << roz << endl; if(sakt[p] > sost[p]) { ost = akt * pot[roz]; // cout << "kupa" << " " << akt << " "<< pot[roz] << endl; w += roz; } else if(sakt[p] < sost[p]) { ost = akt * pot[roz + 1]; w += roz + 1; } } else { // cout << "FLAG" << " " << p << " " << d << " " << sost[p] << " " << sakt[p] << " " << sost.size() << " " << sakt.size() << endl; if(d < p) { ost = akt * pot[roz + 1]; //cout << akt << " " << pot[roz + 1] << endl; w+= roz + 1; } else { LL nowa = 0; int it = 0; //cout << "KUPCIA" << sost << endl; while(it != d) { nowa = (nowa + sost[it] - '0') * 10; it++; } //cout << nowa << endl; nowa = (nowa + sost[it] - '0' + 1); nowa *= pot[sost.size() - it - 1]; ost = nowa; w += roz; } } //cout << sost << endl; } else ost = akt; sost = to_string(ost); il=sost.size(); } else { //cout << "FAJER" << " " <<co << " " << p << " " <<teraz << " " << poprzedni << " " << teraz[p] << " " << poprzedni[p] << endl; if(co == 0) { if(teraz[p] > tmpv2[p]) w += il - teraz.size(); else { il++; w += il - teraz.size(); } } else w += il - teraz.size(); } //cout << w << " " << il << " " << teraz << " " << tmpv2 << endl; if(flag) tmpv2 = teraz; } cout << w; } |