#include <string>
#include <iostream>
//std::string a[300000];
int main(){
std::string a[200010];
int N;
long long int result, diff;
std::string th, bf, tmp;
bool th_base_is_grt, th_base_is_lwr, is_only_nine;
std::cin >> N;
for(int i = 0; i < N; i++){
std::cin >> a[i];
}
result = 0;
for(int i = 1; i < N; i++){
th = a[i];
bf = a[i - 1];
if(th.size() == bf.size()){
if(th < bf){
a[i] += "0";
result++;
}
else if(th == bf){
a[i] += "0";
result++;
}
}
else if(th.size() < bf.size())
{
th_base_is_grt = false;
th_base_is_lwr = false;
for(int j = 0; j < th.size(); j++){
if(th[j] < bf[j]){
th_base_is_lwr = true;
break;
}
else if(th[j] > bf[j]){
th_base_is_grt = true;
break;
}
}
diff = bf.size() - th.size();
if(th_base_is_grt){
for(int k = 0; k < diff; k++)
a[i] += "0";
result += diff;
}
else if(th_base_is_lwr){
for(int k = 0; k < diff + 1; k++)
a[i] += "0";
result += diff + 1;
}
else{ // th_base is equal to bf_base
tmp = "";
is_only_nine = true;
for(int k = bf.size() - diff; k < bf.size(); k++){
tmp += bf[k];
if(bf[k] != '9')
is_only_nine = false;
}
if(!is_only_nine){
for(int k = tmp.size() - 1; k >= 0; k--){
if(tmp[k] != '9'){
tmp[k]++;
break;
}
else
tmp[k]='0';
}
a[i] += tmp;
result += diff;
}
else{
for(int k = 0; k < diff + 1; k++)
a[i] += "0";
result += diff + 1;
}
}
}
}
std::cout << result;
}
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 | #include <string> #include <iostream> //std::string a[300000]; int main(){ std::string a[200010]; int N; long long int result, diff; std::string th, bf, tmp; bool th_base_is_grt, th_base_is_lwr, is_only_nine; std::cin >> N; for(int i = 0; i < N; i++){ std::cin >> a[i]; } result = 0; for(int i = 1; i < N; i++){ th = a[i]; bf = a[i - 1]; if(th.size() == bf.size()){ if(th < bf){ a[i] += "0"; result++; } else if(th == bf){ a[i] += "0"; result++; } } else if(th.size() < bf.size()) { th_base_is_grt = false; th_base_is_lwr = false; for(int j = 0; j < th.size(); j++){ if(th[j] < bf[j]){ th_base_is_lwr = true; break; } else if(th[j] > bf[j]){ th_base_is_grt = true; break; } } diff = bf.size() - th.size(); if(th_base_is_grt){ for(int k = 0; k < diff; k++) a[i] += "0"; result += diff; } else if(th_base_is_lwr){ for(int k = 0; k < diff + 1; k++) a[i] += "0"; result += diff + 1; } else{ // th_base is equal to bf_base tmp = ""; is_only_nine = true; for(int k = bf.size() - diff; k < bf.size(); k++){ tmp += bf[k]; if(bf[k] != '9') is_only_nine = false; } if(!is_only_nine){ for(int k = tmp.size() - 1; k >= 0; k--){ if(tmp[k] != '9'){ tmp[k]++; break; } else tmp[k]='0'; } a[i] += tmp; result += diff; } else{ for(int k = 0; k < diff + 1; k++) a[i] += "0"; result += diff + 1; } } } } std::cout << result; } |
English