#include <bits/stdc++.h> using namespace std; #define MAXN 200007 char tab[MAXN]; long long result = 0ll; long long zeros_at_end = 0ll; long long suffix = 0ll; string previous; int previous_length(){ return previous.length() + zeros_at_end; } char previous_i(int i){ if (i < previous.length()){ return previous[i]; } return 1/0; return '0'; // TODO } string add_extra_zeros(string prefix, long long number){ while(prefix.length() < 10 && number > 0){ prefix.push_back('0'); result++; number--; } result += number; suffix = 0ll; zeros_at_end = number; return prefix; } string lowest_bigger(string prefix){ if (prefix.length() > previous_length()){ zeros_at_end = 0ll; suffix = 0ll; return prefix; } bool prefix_lower = false; bool prefix_higher = false; for(int i = 0; i < prefix.length(); i++) { if (prefix[i] != previous_i(i)){ if (prefix[i] > previous_i(i)){ prefix_higher = true; } else { prefix_lower = true; } break; } } if (!prefix_lower && !prefix_higher){ //check for 9999 bool all_nines = true; int non_nine_position = -1; long long suffixs_prefix = suffix; for (int i = previous_length() - 1; i >= prefix.length(); i--) { if (i >= previous.length()) { if (suffixs_prefix % 10 != 9){ all_nines = false; non_nine_position = i; break; } suffixs_prefix /= 10; } else { if (previous[i] != '9'){ all_nines = false; non_nine_position = i; break; } } } if (all_nines) { long long extra_zeros = previous_length() - prefix.length() + 1; prefix = add_extra_zeros(prefix, extra_zeros); } else { result += previous_length() - prefix.length(); prefix = previous; suffix++; return prefix; } } else if (prefix_lower) { long long extra_zeros = previous_length() - prefix.length() + 1; prefix = add_extra_zeros(prefix, extra_zeros); } else { long long extra_zeros = previous_length() - prefix.length(); prefix = add_extra_zeros(prefix, extra_zeros); } return prefix; } int main() { int n; scanf("%d", &n); scanf("%s", tab); previous = (string)(tab); n--; while(n--){ scanf("%s", tab); previous = lowest_bigger(string(tab)); //printf("Input %s, next: %s %d\n", tab, previous.c_str(), zeros_at_end); } printf("%lld\n", 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 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <bits/stdc++.h> using namespace std; #define MAXN 200007 char tab[MAXN]; long long result = 0ll; long long zeros_at_end = 0ll; long long suffix = 0ll; string previous; int previous_length(){ return previous.length() + zeros_at_end; } char previous_i(int i){ if (i < previous.length()){ return previous[i]; } return 1/0; return '0'; // TODO } string add_extra_zeros(string prefix, long long number){ while(prefix.length() < 10 && number > 0){ prefix.push_back('0'); result++; number--; } result += number; suffix = 0ll; zeros_at_end = number; return prefix; } string lowest_bigger(string prefix){ if (prefix.length() > previous_length()){ zeros_at_end = 0ll; suffix = 0ll; return prefix; } bool prefix_lower = false; bool prefix_higher = false; for(int i = 0; i < prefix.length(); i++) { if (prefix[i] != previous_i(i)){ if (prefix[i] > previous_i(i)){ prefix_higher = true; } else { prefix_lower = true; } break; } } if (!prefix_lower && !prefix_higher){ //check for 9999 bool all_nines = true; int non_nine_position = -1; long long suffixs_prefix = suffix; for (int i = previous_length() - 1; i >= prefix.length(); i--) { if (i >= previous.length()) { if (suffixs_prefix % 10 != 9){ all_nines = false; non_nine_position = i; break; } suffixs_prefix /= 10; } else { if (previous[i] != '9'){ all_nines = false; non_nine_position = i; break; } } } if (all_nines) { long long extra_zeros = previous_length() - prefix.length() + 1; prefix = add_extra_zeros(prefix, extra_zeros); } else { result += previous_length() - prefix.length(); prefix = previous; suffix++; return prefix; } } else if (prefix_lower) { long long extra_zeros = previous_length() - prefix.length() + 1; prefix = add_extra_zeros(prefix, extra_zeros); } else { long long extra_zeros = previous_length() - prefix.length(); prefix = add_extra_zeros(prefix, extra_zeros); } return prefix; } int main() { int n; scanf("%d", &n); scanf("%s", tab); previous = (string)(tab); n--; while(n--){ scanf("%s", tab); previous = lowest_bigger(string(tab)); //printf("Input %s, next: %s %d\n", tab, previous.c_str(), zeros_at_end); } printf("%lld\n", result); } |