Advertisement
trafik

Untitled

May 9th, 2022
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <string>
  5. #include <map>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <set>
  9. #define ll long long
  10. #define len(v) (int)v.size()
  11. #define all(v) v.begin(), v.end()
  12. #define int long long
  13. const ll maxn = 2e5 + 10;
  14. const int logn = 20;
  15. const ll inf = 1e15;
  16. const int mod = 1e9 + 7;
  17. using namespace std;
  18.  
  19. struct msk {
  20.     ll cnt, maxw;
  21.     bool operator==(msk v1) {
  22.         return ((v1.cnt == cnt) && (v1.maxw == maxw));
  23.     }
  24. };
  25.  
  26. void solve() {
  27.     int n, S; cin >> n >> S;
  28.     vector<int> cost(n);
  29.     for (int i = 0; i < n; ++i)
  30.         cin >> cost[i];
  31.  
  32.     if (n == 1) {
  33.         cout << "1\n1 1" << endl;
  34.         return;
  35.     }
  36.  
  37.     vector<msk> dp(1 << n, { n + 10, -100 });
  38.     for (int i = 0; i < n; ++i) {
  39.         dp[1ll << i].cnt = 1;
  40.         dp[1ll << i].maxw = S - cost[i];
  41.     }
  42.     dp[0].cnt = 1;
  43.     dp[0].maxw = S;
  44.    
  45.  
  46.  
  47.     for (int mask = 0; mask < (1ll << n); ++mask) {
  48.         for (int i = 0; i < n; ++i) {
  49.             if (!((mask >> i) & 1)) {
  50.                 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]))) {
  51.                     dp[mask ^ (1ll << i)] = { dp[mask].cnt, dp[mask].maxw - cost[i] };
  52.                 }
  53.                 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]))) {
  54.                     dp[mask ^ (1ll << i)] = { dp[mask].cnt + 1, S - cost[i] };
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.  
  61.     int mask = (1ll << n) - 1;
  62.     vector<int> p;
  63.     while (mask > 1) {
  64.         for (int i = 0; i < n; ++i) {
  65.             if ((mask >> i) & 1) {
  66.                 int maskwi = mask ^ (1ll << i);
  67.                 msk v1 = { dp[maskwi].cnt, dp[maskwi].maxw - cost[i] };
  68.                 msk v2 = { dp[maskwi].cnt + 1, S - cost[i] };
  69.                 if (dp[mask] == v1 || dp[mask] == v2) {
  70.                     p.push_back(i + 1);
  71.                     mask = maskwi;
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     vector<vector<int>> ans;
  79.     int curS = S;
  80.     vector<int> tmp;
  81.     for (int i = len(p) - 1; i >= 0; --i) {
  82.         if (curS < cost[p[i] - 1]) {
  83.             ans.push_back(tmp);
  84.             tmp.clear();
  85.             curS = S;
  86.         }
  87.         curS -= cost[p[i] - 1];
  88.         tmp.push_back(p[i]);
  89.     }
  90.     if (len(tmp) > 0)
  91.         ans.push_back(tmp);
  92.  
  93.     cout << len(ans) << endl;
  94.     for (int i = 0; i < len(ans); ++i) {
  95.         if (len(ans[i]) == 0) continue;
  96.         cout << len(ans[i]) << " ";
  97.         for (auto el : ans[i])
  98.             cout << el << " ";
  99.         cout << endl;
  100.     }
  101. }
  102.  
  103. signed main() {
  104.     ios::sync_with_stdio(0);
  105.     cin.tie(0);
  106.     cout.tie(0);
  107.  
  108.     int t; cin >> t;
  109.     while (t--) {
  110.         solve();
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement