#include <bits/stdc++.h> using namespace std; vector<int> as_vector(int x){ vector<int> result; while(x){ result.push_back(x%10); x /= 10; } reverse(result.begin(), result.end()); return result; } int compare(vector<int> act, vector<int> x){ for(int i = 0; i < (int)x.size(); i++){ if(x[i] > act[i]){ return 1; } if(act[i] > x[i]){ return -1; } } return 0; } vector<int> tail; vector<int> act; vector<vector<int>> a(200001); long long added = 0; long long result = 0; void zero_and_add_tail(){ for(int j = 0; j < (int)tail.size(); j++){ tail[j] = 0; } if(tail.size() < 8){ tail.push_back(0); } } void fix_first_nine_in_act(int i){ int last_not_nine = -1; for(int j = a[i].size(); j < (int)act.size(); j++){ if(act[j] != 9){ last_not_nine = j; } } if(last_not_nine == -1){ for(int j = a[i].size(); j < (int)act.size(); j++){ act[j] = 0; } if(act.size() < 9){ act.push_back(0); } else{ added++; zero_and_add_tail(); } } else{ act[last_not_nine]++; for(int j = last_not_nine + 1; j < (int)act.size(); j++){ act[j] = 0; } for(int j = 0; j < (int)tail.size(); j++){ tail[j] = 0; } } } int main(){ std::ios::sync_with_stdio(false); int n; cin>>n; for(int i = 0; i < n; i++){ int x; cin>>x; a[i] = as_vector(x); } act = a[0]; for(int i = 1; i < n; i++){ if(a[i].size() > act.size()){ act = a[i]; continue; } int size = a[i].size(); int comp = compare(act, a[i]); if(comp == 1){ for(int j = 0; j < (int)act.size() - size; j++){ a[i].push_back(0); } act = a[i]; } if(comp == -1){ for(int j = 0; j < ((int)act.size() - size); j++){ a[i].push_back(0); } act = a[i]; if(act.size() < 9){ act.push_back(0); } else{ added++; zero_and_add_tail(); } } if(comp == 0){ if(tail.size() == 0){ fix_first_nine_in_act(i); } else{ int last_not_nine = -1; for(int j = 0; j < (int)tail.size(); j++){ if(tail[j] != 9){ last_not_nine = j; } } if(last_not_nine == -1){ fix_first_nine_in_act(i); } else{ tail[last_not_nine]++; for(int j = last_not_nine + 1; j < (int)tail.size(); j++){ tail[j] = 0; } } } } result += act.size() - size; result += added; } cout<<result<<endl; }
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 | #include <bits/stdc++.h> using namespace std; vector<int> as_vector(int x){ vector<int> result; while(x){ result.push_back(x%10); x /= 10; } reverse(result.begin(), result.end()); return result; } int compare(vector<int> act, vector<int> x){ for(int i = 0; i < (int)x.size(); i++){ if(x[i] > act[i]){ return 1; } if(act[i] > x[i]){ return -1; } } return 0; } vector<int> tail; vector<int> act; vector<vector<int>> a(200001); long long added = 0; long long result = 0; void zero_and_add_tail(){ for(int j = 0; j < (int)tail.size(); j++){ tail[j] = 0; } if(tail.size() < 8){ tail.push_back(0); } } void fix_first_nine_in_act(int i){ int last_not_nine = -1; for(int j = a[i].size(); j < (int)act.size(); j++){ if(act[j] != 9){ last_not_nine = j; } } if(last_not_nine == -1){ for(int j = a[i].size(); j < (int)act.size(); j++){ act[j] = 0; } if(act.size() < 9){ act.push_back(0); } else{ added++; zero_and_add_tail(); } } else{ act[last_not_nine]++; for(int j = last_not_nine + 1; j < (int)act.size(); j++){ act[j] = 0; } for(int j = 0; j < (int)tail.size(); j++){ tail[j] = 0; } } } int main(){ std::ios::sync_with_stdio(false); int n; cin>>n; for(int i = 0; i < n; i++){ int x; cin>>x; a[i] = as_vector(x); } act = a[0]; for(int i = 1; i < n; i++){ if(a[i].size() > act.size()){ act = a[i]; continue; } int size = a[i].size(); int comp = compare(act, a[i]); if(comp == 1){ for(int j = 0; j < (int)act.size() - size; j++){ a[i].push_back(0); } act = a[i]; } if(comp == -1){ for(int j = 0; j < ((int)act.size() - size); j++){ a[i].push_back(0); } act = a[i]; if(act.size() < 9){ act.push_back(0); } else{ added++; zero_and_add_tail(); } } if(comp == 0){ if(tail.size() == 0){ fix_first_nine_in_act(i); } else{ int last_not_nine = -1; for(int j = 0; j < (int)tail.size(); j++){ if(tail[j] != 9){ last_not_nine = j; } } if(last_not_nine == -1){ fix_first_nine_in_act(i); } else{ tail[last_not_nine]++; for(int j = last_not_nine + 1; j < (int)tail.size(); j++){ tail[j] = 0; } } } } result += act.size() - size; result += added; } cout<<result<<endl; } |