while (t--) { int n, m, k, r; cin >> n >> m >> k >> r;
vector<ll> a(n), c(n); for (int i = 0; i < n; i++) { cin >> a[i] >> c[i]; }
vector M(1U << n, vector<ll>(1U << n)); for (unsigned s = 0; s < 1U << n; s++) { for (unsigned t = 0; t < 1U << n; t++) { ll tota = 0, totc = 0; for (int bit = 0; bit < n; bit++) { if (t >> bit & 1) { totc += c[bit] + k * (s >> bit & 1); tota += a[bit]; } } if (totc <= m) { M[s][t] = tota; } } }
vector Mr(1U << n, vector<ll>(1U << n)); for (; r; r >>= 1) { if (r & 1) Mr = Mr * M; M = M * M; } ll max = 0; for (int i = 0; i < 1U << n; i++) { max = std::max(max, ranges::max(Mr[i])); } cout << max << endl; }
vector<array<ll, 3>> A; A.reserve(N + M); for (int i = 0; i < N; i++) { int x; cin >> x; A.push_back({x, 0, i}); } for (int i = 0; i < M; i++) { int x; cin >> x; A.push_back({x, 1, i}); } ranges::sort(A, greater());
auto check = [&](ll mid) { // 二分最后一个到达的时间 auto check = [&](bool st) { // 最后一次是送左边还是送右边 vector<array<ll, 3>> res; ll curA = mid - K, curB = mid - 2 * K; if (st) { swap(curA, curB); } for (auto [x, op, id] : A) { if (op == 0) { if (x > curA) { // 不能送了就 check 失败 returnpair(false, vector<array<ll, 3>>()); } res.push_back({curA, op, id}); curA -= 2 * K; } else { if (x > curB) { returnpair(false, vector<array<ll, 3>>()); } res.push_back({curB, op, id}); curB -= 2 * K; } } returnpair(true, res); }; for (int o = 0; o < 2; o++) { // 枚举最后一次是送左边还是送右边 auto [success, res] = check(o); if (success) { returnpair(true, res); } } returnpair(false, vector<array<ll, 3>>()); };
ll lo = 0, hi = 1E16; while (lo < hi) { ll mid = lo + hi >> 1; if (check(mid).first) { hi = mid; } else { lo = mid + 1; } } auto [success, res] = check(lo); ranges::sort(res); cout << lo << '\n'; for (auto [x, op, id] : res) { cout << x << ' ' << op << ' ' << id + 1 << '\n'; }
voideachT(){ int a, b, n, p; cin >> a >> b >> n >> p;
auto solve = [&](int s, int x) { array<int, 8> num{}; num[7] = s / 7; x -= num[7]; s %= 7; if (s == 1) { num[7]--; num[6]++; s++; } if (s != 0) { num[s]++; x--; } num[0] = x; return num; };
auto check = [&](int n, int a, int b) { for (int x = 0; x <= n; x++) { int sa = a; int sb = b;
int y = n - x; sa -= x; sb -= y; if (sa < 0 || sb < 0) continue;
if (sa > 7 * x || sb > 7 * y) continue;
if (sa == 1 || sb == 1) continue;
auto numa = solve(sa, x); auto numb = solve(sb, y);
if (n >= 6) { auto [numa, numb] = check(21 - n, a, b);
if (numa == array{ -1, -1, -1, -1, -1, -1, -1, -1 } && numb == array{ -1, -1, -1, -1, -1, -1, -1, -1 }) { ok = 0; } else { for (int i = 0; i <= 7; i++) { int x = numa[i]; if (x == 0) continue;
for (int _ = 0; _ < x; _++) { if (i == 0) { ans += "1//"; } else { ans += '1'; ans += ('0' + i); } } } ans += '/'; for (int i = 0; i <= 7; i++) { int x = numb[i]; if (x == 0) continue;
for (int _ = 0; _ < x; _++) { if (i == 0) { ans += "1//"; } else { ans += '1'; ans += ('0' + i); } } } if (p == 0) ans += '/'; } } else { int x = 6 - n; array<int, 6> s{ 2, 3, 4, 5, 6, 7 }; int okk = 0; for (int mask = 0; mask < 1 << x; mask++) { int sa = a, sb = b; for (int bit = 0; bit < x; bit++) { if (mask >> bit & 1) { sa -= s[bit]; } else { sb -= s[bit]; } }
if (n == 0) { if ((mask >> (x - 1) & 1) == p) { continue; } }
for (int i = 0; i <= 7; i++) { int x = numa[i]; if (x == 0) continue;
for (int _ = 0; _ < x; _++) { if (i == 0) { ans += "1//"; } else { ans += '1'; ans += ('0' + i); } } } ans += '/'; for (int i = 0; i <= 7; i++) { int x = numb[i]; if (x == 0) continue;
for (int _ = 0; _ < x; _++) { if (i == 0) { ans += "1//"; } else { ans += '1'; ans += ('0' + i); } } }
int f = 1;
for (int bit = 0; bit < x; bit++) { if (mask >> bit & 1) { if (f == 1) { ans += '/'; f = 0; } ans += ('0' + s[bit]);
} else { if (f == 0) { ans += '/'; f = 1; } ans += ('0' + s[bit]); } }
if (f != p) ans += '/';
okk = 1; break; } ok &= okk; }
if (ok) { cout << ans << endl; } else { cout << "NA" << endl; } }
// 线段树 constexprint inf = 0x3f3f3f3f; structLazy { int add{}; voidapply(const Lazy& x){ add += x.add; } }; structInfo { int max = -inf; voidapply(const Lazy& x){ max += x.add; } }; Info operator + (const Info& l, const Info& r) { return { max(l.max, r.max) }; }
voidsolve(){ int N, L, K; cin >> N >> L >> K;
vector<int> l(N); for (int i = 0; i < N; i++) { cin >> l[i]; } ranges::sort(l);
vector<int> d(6 * N); // 尽可能开大一点 for (int i = 0; i < N; i++) { d[l[i]]++; d[l[i] + L]--; } for (int j = 1; j < d.size(); j++) { d[j] += d[j - 1]; }
LazySegmentTree<Info, Lazy> cost(N, {0}); auto add = [&](int val, int lo, int hi) { // (lo, hi] int i = ranges::upper_bound(l, lo) - l.begin(); int j = ranges::upper_bound(l, hi) - l.begin(); // [i, j) cost.update(i, j, {val}); };
vector<int> ex(d.size()); // exclude a segment vector<int> in(d.size()); // include a segment int tot{}; for (int j = 0; j < d.size(); j++) { ex[j] = (d[j] == K + 1) - (d[j] == K); in[j] = (d[j] == K - 1) - (d[j] == K); tot += (d[j] == K); add(ex[j], j - L, j); // 初始移动到无穷远 }
int res = tot; for (int x = -L; x < 0; x++) { add(+in[x + L], -1, x); add(-ex[x + L], x, x + L); add(+in[x + L], x + L, d.size()); } for (int x = 0; x + L < d.size(); x++) { res = max(res, tot + cost.query(0, N).max); add(-in[x], -1, x - L); // x to x + 1 add(+ex[x], x - L, x); add(-in[x], x, d.size()); add(+in[x + L], -1, x); add(-ex[x + L], x, x + L); add(+in[x + L], x + L, d.size()); } cout << res << '\n'; }