#include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #define PB push_back #define MAX 1000000000 typedef long long int ull; using namespace std; bool compare(vector<int> a, vector<int>b) { if (a.size() < b.size()) { return true; } if (a.size() > b.size()) { return false; } for (int i = 0; i < a.size(); ++i) { if (a[i] < b[i]) { return true; } if (a[i] > b[i]) { return false; } } } vector<int> add(vector<int> a) { int k = 1; for (int j = a.size() - 1; j >=0; j--) { a[j] = a[j] + k; k = a[j] / 10; a[j] = a[j]%10; } if (k > 0) { a.insert(a.begin(), k); } return a; } std::pair<vector<int>, ull> find_next(vector<int> previous, vector<int> current) { previous = add(previous); // for(int i = 0; i< previous.size(); ++i) { // printf("%d", previous[i]); // } // printf("\n"); int i = 0; int j = 0; while(true) { // printf("i = %d, j = %d\n", i, j); // printf("i = %d, j = %d\n", previous[i], current[j]); if (j >= current.size()) { return std::make_pair(previous, i + 1); } if (previous[i] < current[j]) { // printf("eeej\n"); int k = previous.size() - current.size(); int a = k; while (k--) { current.PB(0); } return std::make_pair(current, a); } if (previous[i] > current[j]) { // printf("tutaj\n"); int k = previous.size() - current.size() + 1; int a = k; while (k--) { current.PB(0); } return std::make_pair(current, a); } j++; i++; } } int main() { long count = 0; long N; scanf ("%ld", &N); vector<int> previous; previous.PB(0); int c; while (N--) { // printf("%d\n", N); scanf ("%d", &c); vector<int> current; while(c > 0) { current.PB(c % 10); c /= 10; } std::reverse(current.begin(), current.end()); if (compare(current, previous)) { std::pair<vector<int>, ull> tt = find_next(previous, current); // printf("%lld\n", tt.second); count += tt.second; previous = tt.first; } else { previous = current; } // for(int i = 0; i< previous.size(); ++i) { // // printf("%d", previous[i]); // } // printf("\n"); } printf("%ld", count); 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 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 | #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #define PB push_back #define MAX 1000000000 typedef long long int ull; using namespace std; bool compare(vector<int> a, vector<int>b) { if (a.size() < b.size()) { return true; } if (a.size() > b.size()) { return false; } for (int i = 0; i < a.size(); ++i) { if (a[i] < b[i]) { return true; } if (a[i] > b[i]) { return false; } } } vector<int> add(vector<int> a) { int k = 1; for (int j = a.size() - 1; j >=0; j--) { a[j] = a[j] + k; k = a[j] / 10; a[j] = a[j]%10; } if (k > 0) { a.insert(a.begin(), k); } return a; } std::pair<vector<int>, ull> find_next(vector<int> previous, vector<int> current) { previous = add(previous); // for(int i = 0; i< previous.size(); ++i) { // printf("%d", previous[i]); // } // printf("\n"); int i = 0; int j = 0; while(true) { // printf("i = %d, j = %d\n", i, j); // printf("i = %d, j = %d\n", previous[i], current[j]); if (j >= current.size()) { return std::make_pair(previous, i + 1); } if (previous[i] < current[j]) { // printf("eeej\n"); int k = previous.size() - current.size(); int a = k; while (k--) { current.PB(0); } return std::make_pair(current, a); } if (previous[i] > current[j]) { // printf("tutaj\n"); int k = previous.size() - current.size() + 1; int a = k; while (k--) { current.PB(0); } return std::make_pair(current, a); } j++; i++; } } int main() { long count = 0; long N; scanf ("%ld", &N); vector<int> previous; previous.PB(0); int c; while (N--) { // printf("%d\n", N); scanf ("%d", &c); vector<int> current; while(c > 0) { current.PB(c % 10); c /= 10; } std::reverse(current.begin(), current.end()); if (compare(current, previous)) { std::pair<vector<int>, ull> tt = find_next(previous, current); // printf("%lld\n", tt.second); count += tt.second; previous = tt.first; } else { previous = current; } // for(int i = 0; i< previous.size(); ++i) { // // printf("%d", previous[i]); // } // printf("\n"); } printf("%ld", count); return 0; } |