#include <cstdio>
#include <vector>
#include <algorithm>
using namespace::std;
const int N = (int) 2e5 + 5;
vector<int> t[N];
int tab[N], n;
long long oper, pot[N], rez;
void zamien(vector<int> &v, int x) {
while (x) {
v.push_back(x % 10);
x /= 10;
}
reverse(v.begin(), v.end());
}
bool zawiera(long long x, int y) { //y zawiera sie w x
if (x == y) return true;
while (x) {
if (x == y) return true;
x /= 10;
}
return false;
}
int dlugosc(long long x) {
int wyn = 0;
while (x) {
wyn++;
x /= 10;
}
return wyn;
}
long long dowal(long long x, int ile) {
int ob = dlugosc(x);
if (ile - ob > 0) x *= pot[ile - ob];
return x;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &tab[i]);
zamien(t[i], tab[i]);
}
oper = tab[1];
pot[0] = 1;
for (int i = 1; i <= 18; i++) pot[i] = pot[i - 1] * 10;
int dl = (int) t[1].size();
for (int i = 2; i <= n; i++) {
if (dl >= 17) {
int kon = (int) min(t[i].size(), t[i - 1].size());
for (int j = 0; j < kon; j++)
if (t[i][j] > t[i - 1][j]) { // wieksze
rez += dl - t[i].size();
break;
} else if (t[i][j] < t[i - 1][j]) { // mniejsze
dl++;
rez += dl - t[i].size();
break;
}
if (zawiera(tab[i - 1], tab[i])) { // gorne dluzsze
rez += dl - t[i].size();
t[i] = t[i - 1];
tab[i] = 0;
for (int w : t[i]) {
tab[i] += w;
tab[i] *= 10;
}
} else if (zawiera(tab[i], tab[i - 1])) rez += dl - t[i].size();
continue;
}
if (tab[i] > oper) {
oper = tab[i];
dl = dlugosc(oper);
rez += dl - t[i].size();
continue;
}
if (zawiera(tab[i - 1], tab[i])) { // gorne dluzsze
if (!zawiera(oper + 1, tab[i])) oper = dowal(tab[i], dl + 1);
else oper++;
dl = dlugosc(oper);
rez += dl - t[i].size();
continue;
}
if (zawiera(tab[i], tab[i - 1])) { // dolne dluzsze
long long kand0 = oper + 1;
if (zawiera(kand0, tab[i])) {
oper = kand0;
} else {
long long kand = dowal(tab[i], dl);
if (kand > oper) oper = kand;
else oper = dowal(tab[i], dl + 1); //co jesli zawiera
}
dl = dlugosc(oper);
rez += dl - t[i].size();
continue;
}
int kon = (int) min(t[i].size(), t[i - 1].size());
for (int j = 0; j < kon; j++)
if (t[i][j] > t[i - 1][j]) { // wieksze
oper = dowal(tab[i], dl);
dl = dlugosc(oper);
break;
} else if (t[i][j] < t[i - 1][j]) { // mniejsze
oper = dowal(tab[i], dl + 1);
dl = dlugosc(oper);
break;
}
rez += dl - t[i].size();
}
printf("%lld", rez);
}
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace::std; const int N = (int) 2e5 + 5; vector<int> t[N]; int tab[N], n; long long oper, pot[N], rez; void zamien(vector<int> &v, int x) { while (x) { v.push_back(x % 10); x /= 10; } reverse(v.begin(), v.end()); } bool zawiera(long long x, int y) { //y zawiera sie w x if (x == y) return true; while (x) { if (x == y) return true; x /= 10; } return false; } int dlugosc(long long x) { int wyn = 0; while (x) { wyn++; x /= 10; } return wyn; } long long dowal(long long x, int ile) { int ob = dlugosc(x); if (ile - ob > 0) x *= pot[ile - ob]; return x; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &tab[i]); zamien(t[i], tab[i]); } oper = tab[1]; pot[0] = 1; for (int i = 1; i <= 18; i++) pot[i] = pot[i - 1] * 10; int dl = (int) t[1].size(); for (int i = 2; i <= n; i++) { if (dl >= 17) { int kon = (int) min(t[i].size(), t[i - 1].size()); for (int j = 0; j < kon; j++) if (t[i][j] > t[i - 1][j]) { // wieksze rez += dl - t[i].size(); break; } else if (t[i][j] < t[i - 1][j]) { // mniejsze dl++; rez += dl - t[i].size(); break; } if (zawiera(tab[i - 1], tab[i])) { // gorne dluzsze rez += dl - t[i].size(); t[i] = t[i - 1]; tab[i] = 0; for (int w : t[i]) { tab[i] += w; tab[i] *= 10; } } else if (zawiera(tab[i], tab[i - 1])) rez += dl - t[i].size(); continue; } if (tab[i] > oper) { oper = tab[i]; dl = dlugosc(oper); rez += dl - t[i].size(); continue; } if (zawiera(tab[i - 1], tab[i])) { // gorne dluzsze if (!zawiera(oper + 1, tab[i])) oper = dowal(tab[i], dl + 1); else oper++; dl = dlugosc(oper); rez += dl - t[i].size(); continue; } if (zawiera(tab[i], tab[i - 1])) { // dolne dluzsze long long kand0 = oper + 1; if (zawiera(kand0, tab[i])) { oper = kand0; } else { long long kand = dowal(tab[i], dl); if (kand > oper) oper = kand; else oper = dowal(tab[i], dl + 1); //co jesli zawiera } dl = dlugosc(oper); rez += dl - t[i].size(); continue; } int kon = (int) min(t[i].size(), t[i - 1].size()); for (int j = 0; j < kon; j++) if (t[i][j] > t[i - 1][j]) { // wieksze oper = dowal(tab[i], dl); dl = dlugosc(oper); break; } else if (t[i][j] < t[i - 1][j]) { // mniejsze oper = dowal(tab[i], dl + 1); dl = dlugosc(oper); break; } rez += dl - t[i].size(); } printf("%lld", rez); } |
English