#include <cstdio> #include <vector> using namespace std; void Increase(vector<int>& last) { for (int i=0; i<last.size(); ++i) { if (last[i] != 9) { ++last[i]; return; } else { last[i] = 0; } } last.push_back(1); return; } int WillFit(const vector<int>& last, const vector<int>& curr, int c) { for (int i=0; i<c; ++i) { if (curr[c-i-1] > last[last.size()-i-1]) { return 1; } if (curr[c-i-1] < last[last.size()-i-1]) { return -1; } } return 0; } void Clear(vector<int>& last) { for (int i=0; i<min(10, (int)last.size()); ++i) { last[i] = 0; last[last.size()-i-1] = 0; } } void Rewrite(vector<int>& last, const vector<int>& curr, int c) { for (int i=0; i<c; ++i) { last[last.size()-c+i] = curr[i]; } } void Print(const vector<int>& last) { for (int i=last.size()-1; i>=0; --i) { printf("%d", last[i]); } printf("\n"); } int main() { long long result = 0; vector<int> last(1, 0); vector<int> curr(10, 0); int c = 0; int n; scanf("%d", &n); for (int i=0; i<n; ++i) { int v; scanf("%d", &v); c = 0; while (v != 0) { curr[c++] = v % 10; v /= 10; } Increase(last); //printf("Before\n"); //Print(last); //Print(curr); if (c > last.size()) { last.resize(c); Rewrite(last, curr, c); } switch (WillFit(last, curr, c)) { case -1: //printf("=== -1\n"); Clear(last); last.push_back(0); result += last.size() - c; Rewrite(last, curr, c); break; case 0: //printf("=== 0\n"); result += last.size() - c; break; case 1: //printf("=== 1\n"); Clear(last); result += last.size() - c; Rewrite(last, curr, c); break; } //printf("After\n"); //Print(last); } printf("%lld\n", result); return 0; }
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 | #include <cstdio> #include <vector> using namespace std; void Increase(vector<int>& last) { for (int i=0; i<last.size(); ++i) { if (last[i] != 9) { ++last[i]; return; } else { last[i] = 0; } } last.push_back(1); return; } int WillFit(const vector<int>& last, const vector<int>& curr, int c) { for (int i=0; i<c; ++i) { if (curr[c-i-1] > last[last.size()-i-1]) { return 1; } if (curr[c-i-1] < last[last.size()-i-1]) { return -1; } } return 0; } void Clear(vector<int>& last) { for (int i=0; i<min(10, (int)last.size()); ++i) { last[i] = 0; last[last.size()-i-1] = 0; } } void Rewrite(vector<int>& last, const vector<int>& curr, int c) { for (int i=0; i<c; ++i) { last[last.size()-c+i] = curr[i]; } } void Print(const vector<int>& last) { for (int i=last.size()-1; i>=0; --i) { printf("%d", last[i]); } printf("\n"); } int main() { long long result = 0; vector<int> last(1, 0); vector<int> curr(10, 0); int c = 0; int n; scanf("%d", &n); for (int i=0; i<n; ++i) { int v; scanf("%d", &v); c = 0; while (v != 0) { curr[c++] = v % 10; v /= 10; } Increase(last); //printf("Before\n"); //Print(last); //Print(curr); if (c > last.size()) { last.resize(c); Rewrite(last, curr, c); } switch (WillFit(last, curr, c)) { case -1: //printf("=== -1\n"); Clear(last); last.push_back(0); result += last.size() - c; Rewrite(last, curr, c); break; case 0: //printf("=== 0\n"); result += last.size() - c; break; case 1: //printf("=== 1\n"); Clear(last); result += last.size() - c; Rewrite(last, curr, c); break; } //printf("After\n"); //Print(last); } printf("%lld\n", result); return 0; } |