#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll findLen(ll x){
ll res = 0;
while(x > 0){
res++; x /= 10;
}
return res;
}
ll tens(ll x){
ll res = 1;
for(int i=0; i<x; i++)
res *= 10;
return res;
}
int check(ll a, ll alen, ll l, ll llen){
l /= tens(llen-alen);
ll it = tens(alen-1);
while(it > 0){
if((l/it) > (a/it))
return 0;
if((l/it) < (a/it))
return 2;
it /= 10;
}
return 1;
}
int main(){
int n; scanf("%d", &n);
ll L; scanf("%lld", &L);
ll llen=findLen(L), res=0;
for(int i=1; i<n; i++){
ll a; scanf("%lld", &a);
ll alen = findLen(a);
if(L == a){
L *= 10;
res++; llen++;
} else if(L < a){
L = a;
llen = alen;
} else {
int c = check(a, alen, L, findLen(L));
if(c == 0){
L = a; L *= tens(min(16LL, llen)-alen+1);
res += llen-alen+1; llen++;
} else if(c == 1){
ll it = 10; bool flag = false;
for(int j=0; j<findLen(L)-alen; j++){
ll M = L;
M %= it; it /= 10;
M /= it; it *= 100;
if(M != 9){
flag = true;
break;
}
}
if(flag){
L++;
res += llen-alen;
} else {
L = a; L *= tens(min(16LL, llen)-alen+1);
res += llen-alen+1; llen++;
}
} else {
L = a; L *= tens(min(17LL, llen)-alen);
res += llen-alen;
}
}
}
printf("%lld\n", res);
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; ll findLen(ll x){ ll res = 0; while(x > 0){ res++; x /= 10; } return res; } ll tens(ll x){ ll res = 1; for(int i=0; i<x; i++) res *= 10; return res; } int check(ll a, ll alen, ll l, ll llen){ l /= tens(llen-alen); ll it = tens(alen-1); while(it > 0){ if((l/it) > (a/it)) return 0; if((l/it) < (a/it)) return 2; it /= 10; } return 1; } int main(){ int n; scanf("%d", &n); ll L; scanf("%lld", &L); ll llen=findLen(L), res=0; for(int i=1; i<n; i++){ ll a; scanf("%lld", &a); ll alen = findLen(a); if(L == a){ L *= 10; res++; llen++; } else if(L < a){ L = a; llen = alen; } else { int c = check(a, alen, L, findLen(L)); if(c == 0){ L = a; L *= tens(min(16LL, llen)-alen+1); res += llen-alen+1; llen++; } else if(c == 1){ ll it = 10; bool flag = false; for(int j=0; j<findLen(L)-alen; j++){ ll M = L; M %= it; it /= 10; M /= it; it *= 100; if(M != 9){ flag = true; break; } } if(flag){ L++; res += llen-alen; } else { L = a; L *= tens(min(16LL, llen)-alen+1); res += llen-alen+1; llen++; } } else { L = a; L *= tens(min(17LL, llen)-alen); res += llen-alen; } } } printf("%lld\n", res); } |
English