結果
問題 | No.3000 Optimal Run Length Encoding |
ユーザー | 👑 hos.lyric |
提出日時 | 2024-12-25 01:15:50 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2,107 ms / 10,000 ms |
コード長 | 15,647 bytes |
コンパイル時間 | 3,263 ms |
コンパイル使用メモリ | 178,560 KB |
実行使用メモリ | 353,272 KB |
最終ジャッジ日時 | 2024-12-25 19:52:06 |
合計ジャッジ時間 | 87,090 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 268 ms
5,248 KB |
testcase_02 | AC | 262 ms
5,248 KB |
testcase_03 | AC | 256 ms
5,248 KB |
testcase_04 | AC | 261 ms
5,248 KB |
testcase_05 | AC | 235 ms
5,248 KB |
testcase_06 | AC | 248 ms
5,248 KB |
testcase_07 | AC | 261 ms
5,248 KB |
testcase_08 | AC | 258 ms
5,248 KB |
testcase_09 | AC | 254 ms
5,248 KB |
testcase_10 | AC | 233 ms
5,248 KB |
testcase_11 | AC | 252 ms
5,248 KB |
testcase_12 | AC | 257 ms
5,248 KB |
testcase_13 | AC | 235 ms
5,248 KB |
testcase_14 | AC | 222 ms
5,248 KB |
testcase_15 | AC | 222 ms
5,248 KB |
testcase_16 | AC | 236 ms
5,248 KB |
testcase_17 | AC | 215 ms
5,248 KB |
testcase_18 | AC | 146 ms
5,248 KB |
testcase_19 | AC | 344 ms
5,248 KB |
testcase_20 | AC | 341 ms
5,248 KB |
testcase_21 | AC | 337 ms
5,248 KB |
testcase_22 | AC | 351 ms
5,248 KB |
testcase_23 | AC | 337 ms
5,248 KB |
testcase_24 | AC | 363 ms
5,248 KB |
testcase_25 | AC | 332 ms
5,248 KB |
testcase_26 | AC | 343 ms
5,248 KB |
testcase_27 | AC | 296 ms
5,248 KB |
testcase_28 | AC | 354 ms
5,248 KB |
testcase_29 | AC | 333 ms
5,248 KB |
testcase_30 | AC | 365 ms
5,248 KB |
testcase_31 | AC | 338 ms
5,248 KB |
testcase_32 | AC | 281 ms
5,248 KB |
testcase_33 | AC | 275 ms
7,164 KB |
testcase_34 | AC | 342 ms
36,964 KB |
testcase_35 | AC | 282 ms
93,024 KB |
testcase_36 | AC | 280 ms
92,916 KB |
testcase_37 | AC | 285 ms
92,768 KB |
testcase_38 | AC | 298 ms
92,780 KB |
testcase_39 | AC | 277 ms
92,896 KB |
testcase_40 | AC | 295 ms
92,904 KB |
testcase_41 | AC | 294 ms
92,904 KB |
testcase_42 | AC | 305 ms
92,772 KB |
testcase_43 | AC | 323 ms
92,920 KB |
testcase_44 | AC | 282 ms
92,784 KB |
testcase_45 | AC | 2,033 ms
353,272 KB |
testcase_46 | AC | 1,462 ms
205,460 KB |
testcase_47 | AC | 1,721 ms
304,612 KB |
testcase_48 | AC | 1,495 ms
247,088 KB |
testcase_49 | AC | 1,754 ms
264,780 KB |
testcase_50 | AC | 1,635 ms
240,336 KB |
testcase_51 | AC | 1,649 ms
288,228 KB |
testcase_52 | AC | 1,521 ms
254,752 KB |
testcase_53 | AC | 1,811 ms
317,076 KB |
testcase_54 | AC | 1,355 ms
218,792 KB |
testcase_55 | AC | 2,107 ms
353,144 KB |
testcase_56 | AC | 1,630 ms
267,244 KB |
testcase_57 | AC | 1,516 ms
257,124 KB |
testcase_58 | AC | 1,346 ms
209,280 KB |
testcase_59 | AC | 1,808 ms
304,004 KB |
testcase_60 | AC | 2,064 ms
353,268 KB |
testcase_61 | AC | 1,826 ms
295,528 KB |
testcase_62 | AC | 1,371 ms
216,620 KB |
testcase_63 | AC | 1,637 ms
258,216 KB |
testcase_64 | AC | 1,357 ms
205,312 KB |
testcase_65 | AC | 647 ms
259,660 KB |
testcase_66 | AC | 397 ms
114,996 KB |
testcase_67 | AC | 492 ms
172,972 KB |
testcase_68 | AC | 320 ms
107,640 KB |
testcase_69 | AC | 705 ms
217,872 KB |
testcase_70 | AC | 220 ms
103,840 KB |
testcase_71 | AC | 274 ms
94,388 KB |
testcase_72 | AC | 555 ms
171,056 KB |
testcase_73 | AC | 310 ms
98,424 KB |
testcase_74 | AC | 587 ms
170,140 KB |
testcase_75 | AC | 282 ms
93,896 KB |
testcase_76 | AC | 637 ms
174,104 KB |
testcase_77 | AC | 254 ms
93,384 KB |
testcase_78 | AC | 514 ms
179,144 KB |
testcase_79 | AC | 334 ms
93,932 KB |
testcase_80 | AC | 421 ms
144,184 KB |
testcase_81 | AC | 28 ms
5,248 KB |
testcase_82 | AC | 276 ms
103,972 KB |
testcase_83 | AC | 280 ms
104,100 KB |
testcase_84 | AC | 286 ms
103,968 KB |
testcase_85 | AC | 337 ms
103,844 KB |
testcase_86 | AC | 272 ms
103,940 KB |
testcase_87 | AC | 287 ms
103,812 KB |
testcase_88 | AC | 358 ms
103,816 KB |
testcase_89 | AC | 283 ms
103,976 KB |
testcase_90 | AC | 273 ms
104,092 KB |
testcase_91 | AC | 278 ms
104,104 KB |
testcase_92 | AC | 297 ms
103,852 KB |
testcase_93 | AC | 286 ms
103,924 KB |
testcase_94 | AC | 288 ms
103,724 KB |
testcase_95 | AC | 289 ms
103,772 KB |
testcase_96 | AC | 302 ms
104,332 KB |
testcase_97 | AC | 102 ms
43,192 KB |
testcase_98 | AC | 146 ms
64,416 KB |
testcase_99 | AC | 95 ms
36,228 KB |
testcase_100 | AC | 92 ms
39,508 KB |
testcase_101 | AC | 115 ms
44,952 KB |
testcase_102 | AC | 360 ms
103,300 KB |
testcase_103 | AC | 389 ms
105,204 KB |
testcase_104 | AC | 489 ms
118,588 KB |
testcase_105 | AC | 420 ms
116,016 KB |
testcase_106 | AC | 458 ms
111,384 KB |
testcase_107 | AC | 385 ms
121,052 KB |
testcase_108 | AC | 441 ms
116,968 KB |
testcase_109 | AC | 383 ms
108,556 KB |
testcase_110 | AC | 404 ms
124,684 KB |
testcase_111 | AC | 437 ms
126,124 KB |
testcase_112 | AC | 467 ms
150,416 KB |
testcase_113 | AC | 535 ms
150,140 KB |
testcase_114 | AC | 483 ms
150,156 KB |
testcase_115 | AC | 474 ms
150,000 KB |
testcase_116 | AC | 481 ms
153,392 KB |
testcase_117 | AC | 501 ms
151,876 KB |
testcase_118 | AC | 472 ms
149,740 KB |
testcase_119 | AC | 520 ms
153,380 KB |
testcase_120 | AC | 491 ms
151,544 KB |
testcase_121 | AC | 511 ms
152,324 KB |
testcase_122 | AC | 480 ms
150,996 KB |
testcase_123 | AC | 478 ms
152,056 KB |
testcase_124 | AC | 475 ms
151,932 KB |
testcase_125 | AC | 435 ms
143,260 KB |
testcase_126 | AC | 426 ms
139,248 KB |
testcase_127 | AC | 481 ms
151,588 KB |
testcase_128 | AC | 438 ms
144,064 KB |
testcase_129 | AC | 424 ms
142,136 KB |
testcase_130 | AC | 446 ms
149,480 KB |
testcase_131 | AC | 447 ms
142,584 KB |
testcase_132 | AC | 215 ms
103,848 KB |
testcase_133 | AC | 240 ms
103,844 KB |
testcase_134 | AC | 233 ms
103,968 KB |
testcase_135 | AC | 245 ms
103,992 KB |
testcase_136 | AC | 279 ms
103,924 KB |
testcase_137 | AC | 257 ms
103,928 KB |
testcase_138 | AC | 261 ms
103,948 KB |
testcase_139 | AC | 265 ms
103,968 KB |
testcase_140 | AC | 262 ms
103,972 KB |
testcase_141 | AC | 328 ms
104,072 KB |
testcase_142 | AC | 319 ms
103,976 KB |
ソースコード
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") //////////////////////////////////////////////////////////////////////////////// // SA-IS // String: string, vector<int>, vector<long long> // if sigma <= n, O(n) time, O(n) space // if sigma > n, O(n log n) time, O(n) space template <class String> vector<int> suffixArrayRec(const String &as) { const int n = as.size(); if (n == 0) return {}; const auto minmaxA = minmax_element(as.begin(), as.end()); const auto minA = *minmaxA.first, maxA = *minmaxA.second; if (static_cast<unsigned long long>(maxA) - minA >= static_cast<unsigned long long>(n)) { vector<int> us(n); for (int u = 0; u < n; ++u) us[u] = u; std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (as[u] < as[v]); }); int b = 0; vector<int> bs(n, 0); for (int i = 1; i < n; ++i) { if (as[us[i - 1]] != as[us[i]]) ++b; bs[us[i]] = b; } return suffixArrayRec(bs); } const int sigma = maxA - minA + 1; vector<int> pt(sigma + 1, 0), poss(sigma); for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1]; for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a]; // cmp[u] := (as[u, n) < as[u + 1, n)) vector<bool> cmp(n); cmp[n - 1] = false; for (int u = n - 1; --u >= 0; ) { cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1]; } // ><, nn - id (0 <= id < n) int nn = 0; vector<int> ids(n, 0); int last = n; vector<int> nxt(n, 0); // put ><, from the tail of each bucket vector<int> us(n, 0); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) { ids[u] = ++nn; nxt[u] = last; last = u; us[--poss[as[u] - minA]] = u; } // follow > backwards, from the head of each bucket memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } // follow < backwards, from the tail of each bucket memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0; ) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } if (nn) { int vsLen = 0; vector<int> vs(nn); for (const int u : us) if (ids[u]) vs[vsLen++] = u; int b = 0; vector<int> bs(nn, 0); for (int j = 1; j < nn; ++j) { // as[v1, w1] (< or =) as[v0, w0] int v1 = vs[j - 1], v0 = vs[j]; const int w1 = nxt[v1], w0 = nxt[v0]; if (w1 - v1 != w0 - v0) { ++b; } else { for (; ; ++v1, ++v0) { if (v1 == n) { ++b; break; } if (as[v1] != as[v0]) { ++b; break; } if (v1 == w1) break; } } bs[nn - ids[vs[j]]] = b; } for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u; const auto sub = suffixArrayRec(bs); // put ><, from the tail of each bucket memset(us.data(), 0, n * sizeof(int)); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int j = nn; --j >= 0; ) { const int u = vs[sub[j]]; us[--poss[as[u] - minA]] = u; } // follow > backwards, from the head of each bucket memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } // follow < backwards, from the tail of each bucket memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0; ) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } } return us; } // us[i]: i-th suffix // su[u]: index of as[u, n) // hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n) struct SuffixArray { int n; bool rmq; vector<int> us, su, hs; SuffixArray() : n(0), rmq(false) {} SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); } template <class String> void build(const String &as) { n = as.size(); us = suffixArrayRec(as); su.resize(n + 1); for (int i = 0; i < n; ++i) su[us[i]] = i; su[n] = -1; hs.assign(n, 0); for (int h = 0, u = 0; u < n; ++u) if (su[u]) { for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {} hs[su[u]] = h; if (h) --h; } if (rmq) { const int logN = n ? (31 - __builtin_clz(n)) : 0; hs.resize((logN + 1) * n - (1 << logN) + 1); for (int e = 0; e < logN; ++e) { int *hes = hs.data() + e * n; for (int i = 0; i <= n - (1 << (e + 1)); ++i) { hes[n + i] = min(hes[i], hes[i + (1 << e)]); } } } } // Returns longest common prefix of as[u, n) and as[v, n). // 0 <= u, v <= n // Assumes rmq. inline int lcp(int u, int v) const { if (u == v) return n - u; int i = su[u], j = su[v]; if (i > j) swap(i, j); const int e = 31 - __builtin_clz(j - i); return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]); } }; //////////////////////////////////////////////////////////////////////////////// // https://codeforces.com/blog/entry/106725 // Lyndon // * smaller than non-trivial suffix // * smaller than other cyclic shift // Lyndon factorization (Lyndon factors in non-incr. order): // * longest Lyndon prefix greedily // * min suffix greedily (removing min suffix does not change the suffix order) // pqs[v] = (p, q): Lyndon factorization of as[0, v) ends with a[v - p, v)^q // String: string, vector<int>, vector<long long> // O(n) time // // min_u as[u, v) bs: // for (int u = v; ; ) { // // candidate u // if (v == 0) break; // const int p = pqs[u].first, q = pqs[u].second; // const int uu = u - q * p; // if (!(p >= v - u && sa.lcp(uu, u) >= v - u)) break; // // (v - u) is at least doubled // u = uu; // } // } template <class String> vector<pair<int, int>> lyndonSuffix(const String &as) { const int n = as.size(); vector<pair<int, int>> pqs(n + 1); pqs[0] = make_pair(0, 0); for (int u = 0; u < n; ) { for (int p = 1, q = 1, r = 0, v = u + 1; ; ++v) { // as[u, v) = as[u, u + p)^q as[u, u + r) // as[u, u + p): Lyndon pqs[v] = (r != 0) ? pqs[u + r] : make_pair(p, q); if (v == n || as[v - p] > as[v]) { u = v - r; break; } else if (as[v - p] < as[v]) { p = v + 1 - u; q = 1; r = 0; } else { if (++r == p) { ++q; r = 0; } } } } return pqs; } // as[u, vs[u]): longest Lyndon prefix of as[u, n) // String: string, vector<int>, vector<long long> // O(n) time template <class String> vector<int> lyndonPrefix(const String &as, const SuffixArray &sa) { const int n = as.size(); // top: larger suffix int stackLen = 0; vector<int> stack(n + 1); vector<int> vs(n); for (int u = 0; u <= n; ++u) { for (; stackLen > 0 && sa.su[stack[stackLen - 1]] > sa.su[u]; --stackLen) { vs[stack[stackLen - 1]] = u; } stack[stackLen++] = u; } return vs; } template <class String> vector<int> lyndonPrefix(const String &as) { return lyndonPrefix(as, SuffixArray(as, /*rmq=*/false)); } // lyndonPrefix for invert(as), using suffix array of as template <class String> vector<int> lyndonPrefixInverted(const String &as, const SuffixArray &sa) { assert(sa.rmq); const int n = as.size(); // top: larger suffix int stackLen = 0; vector<int> stack(n + 1); vector<int> vs(n); for (int u = 0; u <= n; ++u) { for (; stackLen > 0; --stackLen) { const int uu = stack[stackLen - 1]; const int l = sa.lcp(uu, u); if (u + l < n && as[uu + l] > as[u + l]) break; vs[uu] = u; } stack[stackLen++] = u; } return vs; } // (p, [u, v)): run <=> // * p: min period of as[u, v) // * v - u >= 2 p // * [u, v): maximal // \sum 1 <= n // \sum (v - u) / p <= 3 n // \sum (v - u - 2 p + 1) \in O(n log n) (TODO: proof) // Returns runs (p, [u, v)) in lex. order. // String: string, vector<int>, vector<long long> // O(n log n) time, (<= 6 n + 2) SuffixArray::lcp calls template <class String> vector<pair<int, pair<int, int>>> repetitions(const String &as, const SuffixArray &sa) { assert(sa.rmq); const int n = as.size(); if (n == 0) return {}; String asRev = as; std::reverse(asRev.begin(), asRev.end()); const SuffixArray saRev(asRev, /*rmq=*/true); const vector<int> vs = lyndonPrefix(as, sa); const vector<int> vsInverted = lyndonPrefixInverted(as, sa); vector<pair<int, pair<int, int>>> runs; for (int u = 0; u < n; ++u) { // from longest lyndon prefix of as[u, n) or invert(as)[u, n) { const int v = vs[u]; const int p = v - u, uu = u - saRev.lcp(n - u, n - v), vv = v + sa.lcp(u, v); if (vv - uu >= 2 * p) runs.emplace_back(p, make_pair(uu, vv)); } if (vs[u] != vsInverted[u]) { const int v = vsInverted[u]; const int p = v - u, uu = u - saRev.lcp(n - u, n - v), vv = v + sa.lcp(u, v); if (vv - uu >= 2 * p) runs.emplace_back(p, make_pair(uu, vv)); } } // radix sort const int runsLen = runs.size(); auto runsWork = runs; vector<int> pt(n + 1, 0); for (int i = 0; i < runsLen; ++i) ++pt[runs[i].second.first]; for (int u = 0; u < n - 1; ++u) pt[u + 1] += pt[u]; for (int i = runsLen; --i >= 0; ) runsWork[--pt[runs[i].second.first]] = runs[i]; memset(pt.data() + 1, 0, n * sizeof(int)); for (int i = 0; i < runsLen; ++i) ++pt[runsWork[i].first]; for (int p = 1; p < n; ++p) pt[p + 1] += pt[p]; for (int i = runsLen; --i >= 0; ) runs[--pt[runsWork[i].first]] = runsWork[i]; runs.erase(std::unique(runs.begin(), runs.end()), runs.end()); return runs; } template <class String> vector<pair<int, pair<int, int>>> repetitions(const String &as) { return repetitions(as, SuffixArray(as, /*rmq=*/true)); } //////////////////////////////////////////////////////////////////////////////// constexpr int INF = 1001001001; constexpr int TEN[7] = { 1, 10, 100, 1000, 10000, 100000, 1000000, }; int N; char S[500'010]; int main() { for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) { scanf("%s", S); N = strlen(S); const auto runs = repetitions(string(S)); // cerr<<"runs = "<<runs<<endl; const int len = runs.size(); vector<vector<int>> headss[6]; vector<vector<vector<pair<int, int>>>> quess[6]; for (int e = 0; e < 6; ++e) { headss[e].assign(len, {}); quess[e].assign(len, {}); } for (int i = 0; i < len; ++i) { const int p = runs[i].first; const int u = runs[i].second.first; const int v = runs[i].second.second; const int lim = (v - u) / p; const int sz = min(v - u - 2*p + 1, p); for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) { headss[e][i].assign(sz, 0); quess[e][i].assign(sz, {}); } } vector<vector<int>> addssL(N + 1), remssL(N + 1); vector<vector<int>> addssR(N + 1), remssR(N + 1); for (int i = 0; i < len; ++i) { const int p = runs[i].first; const int u = runs[i].second.first; const int v = runs[i].second.second; addssL[u].push_back(i); remssL[v - 2*p].push_back(i); addssR[u + 2*p].push_back(i); remssR[v].push_back(i); } pair<int, int> norun(INF, -1); vector<int> dp(N + 1, INF); vector<pair<int, int>> prv(N + 1, make_pair(-1, -1)); set<int> onL, onR; dp[0] = 0; for (int x = 0; x <= N; ++x) { if (x) { // get for (const int i : addssR[x]) onR.insert(i); for (const int i : onR) { const int p = runs[i].first; const int u = runs[i].second.first; const int v = runs[i].second.second; const int lim = (v - u) / p; const int r = (x - u) % p; for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) { int &head = headss[e][i][r]; auto &que = quess[e][i][r]; for (; head < (int)que.size() && que[head].second + (TEN[e + 1] - 1) * p < x; ++head) {} if (head < (int)que.size()) { // cerr<<COLOR("94")<<"get "<<x<<" "<<i<<" "<<que[head]<<COLOR()<<endl; if (chmin(dp[x], que[head].first)) { prv[x] = make_pair(i, que[head].second); } } } } for (const int i : remssR[x]) { onR.erase(i); const int p = runs[i].first; const int u = runs[i].second.first; const int v = runs[i].second.second; const int lim = (v - u) / p; for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) { quess[e][i].clear(); quess[e][i].shrink_to_fit(); } } if (chmin(dp[x], norun.first + x)) { prv[x] = make_pair(-1, norun.second); } } // push { for (const int i : addssL[x]) onL.insert(i); for (const int i : onL) { const int p = runs[i].first; const int u = runs[i].second.first; const int v = runs[i].second.second; const int lim = (v - u) / p; const int r = (x - u) % p; for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) { auto &head = headss[e][i][r]; auto &que = quess[e][i][r]; const pair<int, int> val(dp[x] + p + (e + 1), x); for (; head < (int)que.size() && que.back() >= val; que.pop_back()) {} // cerr<<COLOR("91")<<"push "<<x<<" "<<i<<" "<<val<<COLOR()<<endl; que.push_back(val); } } for (const int i : remssL[x]) onL.erase(i); chmin(norun, make_pair(dp[x] - x + 1, x)); } } // cerr<<"dp = "<<dp<<endl; // cerr<<"prv = "<<prv<<endl; vector<string> ops; for (int x = N; x > 0; ) { const int i = prv[x].first; const int y = prv[x].second; const int p = (~i) ? runs[i].first : (x - y); ops.push_back(string(S + y, S + (y + p)) + to_string((x - y) / p)); x = y; } reverse(ops.begin(), ops.end()); string ans; for (const auto &op : ops) ans += op; puts(ans.c_str()); } #ifndef LOCAL break; #endif } return 0; }