#include <string> #include <iostream> #include <algorithm> using namespace std; int length(int v) { int res = 0; while (v) { res++; v /= 10; } return res; } bool willIncreaseLen(int cnt) { return length(cnt) != length(cnt + 1); } bool increaseString(int sufLen, string &s) { int last = s.size() - 1; for (int i=0; i<sufLen; i++) { if (s[last - i] != '9') { s[last-i]++; for (int j=last-i+1; j<=last; j++) s[j] = '0'; return true; } } return false; } int firstDigit(int &v) { int cp = v, siz = 1; while(cp >= 10) { siz *= 10; cp /= 10; } if (siz >= 10) v %= siz; return cp; } struct Res { string s; int padding; int cnt; int totalLength() { return s.size() + padding; } bool isCntInPadding() { return padding >= length(cnt); } bool isCntFull() { return padding == length(cnt) && willIncreaseLen(cnt); } void resize(int wanted) { int rest = wanted - s.size(); while (padding && rest-- > 0) { int fst = firstDigit(cnt); s += fst + '0'; padding--; } } void normalize() { resize(10); } }; long long makeLarger(Res &last, Res ¤t) { if (last.totalLength() < current.totalLength()) return 0; int to = min(last.s.size(), current.s.size()); int diff = last.s.size() - current.s.size(); for (int i=0; i<to; i++) { char l = last.s[i], c = current.s[i]; if (l != c) { current.padding = last.totalLength() - current.s.size(); if (l > c) current.padding += 1; return current.padding; } } if (diff == 0) { // two equal numbers current.padding = last.padding; current.cnt = last.cnt; bool increase = willIncreaseLen(current.cnt); current.cnt++; if (increase) { if (!current.isCntInPadding()) { // 5||9 -> 5|00|0 current.cnt = 0; current.padding++; } } return current.padding; } else { // 56 and 560 or 560 and 56 int oldSize = current.s.size(); if (!last.isCntFull()) { current.s = last.s; current.cnt = last.cnt + 1; current.padding = last.padding; } else { if (increaseString(last.s.size() - current.s.size(), last.s)) { current.s = last.s; current.padding = last.padding; } else { // last must be ending with 9s, nothing to do here current.padding = last.totalLength() - current.s.size() + 1; } current.cnt = 0; } return current.totalLength() - oldSize; } } int main() { int n; long long res = 0; cin >> n; Res last; last.padding = last.cnt = 0; cin >> last.s; n--; while (n--) { Res current; current.padding = current.cnt = 0; cin >> current.s; res += makeLarger(last, current); last = current; last.normalize(); } cout << res; }
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 | #include <string> #include <iostream> #include <algorithm> using namespace std; int length(int v) { int res = 0; while (v) { res++; v /= 10; } return res; } bool willIncreaseLen(int cnt) { return length(cnt) != length(cnt + 1); } bool increaseString(int sufLen, string &s) { int last = s.size() - 1; for (int i=0; i<sufLen; i++) { if (s[last - i] != '9') { s[last-i]++; for (int j=last-i+1; j<=last; j++) s[j] = '0'; return true; } } return false; } int firstDigit(int &v) { int cp = v, siz = 1; while(cp >= 10) { siz *= 10; cp /= 10; } if (siz >= 10) v %= siz; return cp; } struct Res { string s; int padding; int cnt; int totalLength() { return s.size() + padding; } bool isCntInPadding() { return padding >= length(cnt); } bool isCntFull() { return padding == length(cnt) && willIncreaseLen(cnt); } void resize(int wanted) { int rest = wanted - s.size(); while (padding && rest-- > 0) { int fst = firstDigit(cnt); s += fst + '0'; padding--; } } void normalize() { resize(10); } }; long long makeLarger(Res &last, Res ¤t) { if (last.totalLength() < current.totalLength()) return 0; int to = min(last.s.size(), current.s.size()); int diff = last.s.size() - current.s.size(); for (int i=0; i<to; i++) { char l = last.s[i], c = current.s[i]; if (l != c) { current.padding = last.totalLength() - current.s.size(); if (l > c) current.padding += 1; return current.padding; } } if (diff == 0) { // two equal numbers current.padding = last.padding; current.cnt = last.cnt; bool increase = willIncreaseLen(current.cnt); current.cnt++; if (increase) { if (!current.isCntInPadding()) { // 5||9 -> 5|00|0 current.cnt = 0; current.padding++; } } return current.padding; } else { // 56 and 560 or 560 and 56 int oldSize = current.s.size(); if (!last.isCntFull()) { current.s = last.s; current.cnt = last.cnt + 1; current.padding = last.padding; } else { if (increaseString(last.s.size() - current.s.size(), last.s)) { current.s = last.s; current.padding = last.padding; } else { // last must be ending with 9s, nothing to do here current.padding = last.totalLength() - current.s.size() + 1; } current.cnt = 0; } return current.totalLength() - oldSize; } } int main() { int n; long long res = 0; cin >> n; Res last; last.padding = last.cnt = 0; cin >> last.s; n--; while (n--) { Res current; current.padding = current.cnt = 0; cin >> current.s; res += makeLarger(last, current); last = current; last.normalize(); } cout << res; } |