Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <string>
- #include <map>
- #include <cmath>
- #include <algorithm>
- #include <set>
- #define ll long long
- #define len(v) (int)v.size()
- #define all(v) v.begin(), v.end()
- #define int long long
- const ll maxn = 2e5 + 10;
- const int logn = 20;
- const ll inf = 1e15;
- const int mod = 1e9 + 7;
- using namespace std;
- struct msk {
- ll cnt, maxw;
- bool operator==(msk v1) {
- return ((v1.cnt == cnt) && (v1.maxw == maxw));
- }
- };
- void solve() {
- int n, S; cin >> n >> S;
- vector<int> cost(n);
- for (int i = 0; i < n; ++i)
- cin >> cost[i];
- if (n == 1) {
- cout << "1\n1 1" << endl;
- return;
- }
- vector<msk> dp(1 << n, { n + 10, -100 });
- for (int i = 0; i < n; ++i) {
- dp[1ll << i].cnt = 1;
- dp[1ll << i].maxw = S - cost[i];
- }
- dp[0].cnt = 1;
- dp[0].maxw = S;
- for (int mask = 0; mask < (1ll << n); ++mask) {
- for (int i = 0; i < n; ++i) {
- if (!((mask >> i) & 1)) {
- if (dp[mask].maxw >= cost[i] && (dp[mask ^ (1ll << i)].cnt > dp[mask].cnt || (dp[mask ^ (1ll << i)].cnt == dp[mask].cnt && dp[mask ^ (1ll << i)].maxw < dp[mask].maxw - cost[i]))) {
- dp[mask ^ (1ll << i)] = { dp[mask].cnt, dp[mask].maxw - cost[i] };
- }
- else if (dp[mask].maxw < cost[i] && (dp[mask ^ (1ll << i)].cnt > (dp[mask].cnt + 1) || (dp[mask ^ (1ll << i)].cnt == (dp[mask].cnt + 1) && dp[mask ^ (1ll << i)].maxw < S - cost[i]))) {
- dp[mask ^ (1ll << i)] = { dp[mask].cnt + 1, S - cost[i] };
- }
- }
- }
- }
- int mask = (1ll << n) - 1;
- vector<int> p;
- while (mask > 1) {
- for (int i = 0; i < n; ++i) {
- if ((mask >> i) & 1) {
- int maskwi = mask ^ (1ll << i);
- msk v1 = { dp[maskwi].cnt, dp[maskwi].maxw - cost[i] };
- msk v2 = { dp[maskwi].cnt + 1, S - cost[i] };
- if (dp[mask] == v1 || dp[mask] == v2) {
- p.push_back(i + 1);
- mask = maskwi;
- break;
- }
- }
- }
- }
- vector<vector<int>> ans;
- int curS = S;
- vector<int> tmp;
- for (int i = len(p) - 1; i >= 0; --i) {
- if (curS < cost[p[i] - 1]) {
- ans.push_back(tmp);
- tmp.clear();
- curS = S;
- }
- curS -= cost[p[i] - 1];
- tmp.push_back(p[i]);
- }
- if (len(tmp) > 0)
- ans.push_back(tmp);
- cout << len(ans) << endl;
- for (int i = 0; i < len(ans); ++i) {
- if (len(ans[i]) == 0) continue;
- cout << len(ans[i]) << " ";
- for (auto el : ans[i])
- cout << el << " ";
- cout << endl;
- }
- }
- signed main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t; cin >> t;
- while (t--) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement