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
| #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long double LD; const LD inf = 1e18; const int N = 1e5 + 10, S = 40; char str[N][S]; int n, pre[N]; LD L, P, s[N], f[N]; struct Node { int p, l, r; } q[N]; int hd, tl; void print(int l, int r) { if (l) print(pre[l], l); for (int i = l + 1; i < r; i++) printf("%s ", str[i] + 1); printf("%s\n", str[r] + 1); } LD binpow(LD b, int k) { LD res = 1; while (k) { if (k & 1) res = res * b; b = b * b; k >>= 1; } return res; } LD cal(int i, int j) { return f[j] + binpow(abs(s[i] - s[j] + i - j - 1 - L), P); } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%Lf%Lf", &n, &L, &P); for (int i = 1; i <= n; i++) { scanf("%s", str[i] + 1); s[i] = s[i - 1] + strlen(str[i] + 1); } hd = 1, tl = 0; q[++tl] = (Node){0, 1, n}; for (int i = 1; i <= n; i++) { while (hd <= tl && q[hd].r < i) hd++; pre[i] = q[hd].p; f[i] = cal(i, q[hd].p); int p = n + 1; while (hd <= tl && cal(q[tl].l, i) <= cal(q[tl].l, q[tl].p)) p = q[tl--].l; if (hd <= tl && cal(q[tl].r, i) <= cal(q[tl].r, q[tl].p)) { int l = q[tl].l, r = q[tl].r; while (l <= r) { int mid = l + r >> 1; cal(mid, i) <= cal(mid, q[tl].p) ? (p = mid, r = mid - 1) : l = mid + 1; } q[tl].r = p - 1; } if (p <= n) q[++tl] = {i, p, n}; } if (f[n] > inf) puts("Too hard to arrange"); else { printf("%.0Lf\n", f[n]); print(pre[n], n); } puts("--------------------"); } return 0; }
|