#include <iostream> #include <vector> #include <queue> using namespace std; typedef long long ll; void Wrzuc(deque<char>& D, string a) { D.clear(); for (int i = 0; i < a.size(); ++i) D.push_back(a[i]); } int Porownaj(string a, deque<char>& D) { string t = ""; for (int i = 0; i < a.size(); ++i) { t += D.front(); D.pop_front(); } for (int i = t.size() - 1; i >= 0; --i) D.push_front(t[i]); int wyn; if (a < t) wyn = -1; if (a == t) wyn = 0; if (a > t) wyn = 1; return wyn; } void WrzucDl(deque<char>& D, string a, int zera) { if (D.size() <= 30) { string z(zera, '0'); a = a + z; Wrzuc(D, a); return; } for (int i = 0; i < 20; ++i) D.pop_front(); for (int i = 0; i < 10; ++i) D.pop_back(); if ((int) D.size() <= zera) { int dod = zera - (int) D.size(); for (int i = 0; i < dod; ++i) D.push_front('0'); } else { int usu = (int) D.size() - zera; for (int i = 0; i < usu; ++i) D.pop_front(); } for (int i = a.size() - 1; i >= 0; --i) D.push_front(a[i]); } int PierNiedz(deque<char>& D) { int il = 0; while (D.size() != 0 && D.back() == '9') { D.pop_back(); ++il; } int odp; if (D.size() == 0) odp = -1; else odp = D.size() - 1; for (int i = 0; i < il; ++i) D.push_back('9'); return odp; } void Dodaj1(deque<char>& D, int niedz) { int ildz = D.size() - niedz - 1; for (int i = 0; i < ildz; ++i) D.pop_back(); ++(D.back()); for (int i = 0; i < ildz; ++i) D.push_back('0'); } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; deque<char> D; Wrzuc(D, a[0]); ll odp = 0; for (int i = 1; i < n; ++i) { if (a[i].size() > D.size()) { Wrzuc(D, a[i]); continue; } if (a[i].size() == D.size()) { int por = Porownaj(a[i], D); Wrzuc(D, a[i]); if (por == -1 || por == 0) { D.push_back('0'); ++odp; } continue; } if (a[i].size() < D.size()) { int por = Porownaj(a[i], D); if (por == -1) { odp += (int) D.size() - (int) a[i].size() + 1; WrzucDl(D, a[i], (int) D.size() - (int) a[i].size() + 1); } if (por == 1) { odp += (int) D.size() - (int) a[i].size(); WrzucDl(D, a[i], (int) D.size() - (int) a[i].size()); } if (por == 0) { int niedz = PierNiedz(D); if (niedz < (int) a[i].size()) { odp += (int) D.size() - (int) a[i].size() + 1; int dl = (int) D.size() - (int) a[i].size(); for (int j = 0; j < dl; ++j) D.pop_back(); for (int j = 0; j < dl + 1; ++j) D.push_back('0'); } else { odp += (int) D.size() - (int) a[i].size(); Dodaj1(D, niedz); } } } } cout << odp; 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #include <iostream> #include <vector> #include <queue> using namespace std; typedef long long ll; void Wrzuc(deque<char>& D, string a) { D.clear(); for (int i = 0; i < a.size(); ++i) D.push_back(a[i]); } int Porownaj(string a, deque<char>& D) { string t = ""; for (int i = 0; i < a.size(); ++i) { t += D.front(); D.pop_front(); } for (int i = t.size() - 1; i >= 0; --i) D.push_front(t[i]); int wyn; if (a < t) wyn = -1; if (a == t) wyn = 0; if (a > t) wyn = 1; return wyn; } void WrzucDl(deque<char>& D, string a, int zera) { if (D.size() <= 30) { string z(zera, '0'); a = a + z; Wrzuc(D, a); return; } for (int i = 0; i < 20; ++i) D.pop_front(); for (int i = 0; i < 10; ++i) D.pop_back(); if ((int) D.size() <= zera) { int dod = zera - (int) D.size(); for (int i = 0; i < dod; ++i) D.push_front('0'); } else { int usu = (int) D.size() - zera; for (int i = 0; i < usu; ++i) D.pop_front(); } for (int i = a.size() - 1; i >= 0; --i) D.push_front(a[i]); } int PierNiedz(deque<char>& D) { int il = 0; while (D.size() != 0 && D.back() == '9') { D.pop_back(); ++il; } int odp; if (D.size() == 0) odp = -1; else odp = D.size() - 1; for (int i = 0; i < il; ++i) D.push_back('9'); return odp; } void Dodaj1(deque<char>& D, int niedz) { int ildz = D.size() - niedz - 1; for (int i = 0; i < ildz; ++i) D.pop_back(); ++(D.back()); for (int i = 0; i < ildz; ++i) D.push_back('0'); } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; deque<char> D; Wrzuc(D, a[0]); ll odp = 0; for (int i = 1; i < n; ++i) { if (a[i].size() > D.size()) { Wrzuc(D, a[i]); continue; } if (a[i].size() == D.size()) { int por = Porownaj(a[i], D); Wrzuc(D, a[i]); if (por == -1 || por == 0) { D.push_back('0'); ++odp; } continue; } if (a[i].size() < D.size()) { int por = Porownaj(a[i], D); if (por == -1) { odp += (int) D.size() - (int) a[i].size() + 1; WrzucDl(D, a[i], (int) D.size() - (int) a[i].size() + 1); } if (por == 1) { odp += (int) D.size() - (int) a[i].size(); WrzucDl(D, a[i], (int) D.size() - (int) a[i].size()); } if (por == 0) { int niedz = PierNiedz(D); if (niedz < (int) a[i].size()) { odp += (int) D.size() - (int) a[i].size() + 1; int dl = (int) D.size() - (int) a[i].size(); for (int j = 0; j < dl; ++j) D.pop_back(); for (int j = 0; j < dl + 1; ++j) D.push_back('0'); } else { odp += (int) D.size() - (int) a[i].size(); Dodaj1(D, niedz); } } } } cout << odp; return 0; } |