trafik

Untitled

May 9th, 2022
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 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, { inf, -1 });
  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.                 if ((dp[mask ^ (1ll << i)].cnt > dp[mask].cnt && dp[mask].maxw >= cost[i]) || (dp[mask ^ (1ll << i)].cnt == dp[mask].cnt && dp[mask ^ (1ll << i)].maxw < (dp[mask].maxw - cost[i]))) {
  57.                     dp[mask ^ (1ll << i)] = { dp[mask].cnt, dp[mask].maxw - cost[i] };
  58.                 }
  59.                 else if ((dp[mask ^ (1ll << i)].cnt > (dp[mask].cnt + 1) && dp[mask].maxw < cost[i]) || (dp[mask ^ (1ll << i)].cnt == (dp[mask].cnt + 1) && dp[mask ^ (1ll << i)].maxw < (S - cost[i]))) {
  60.                     dp[mask ^ (1ll << i)] = { dp[mask].cnt + 1, S - cost[i] };
  61.                 }
  62.             }
  63.         }
  64.     }
  65.  
  66.  
  67.     int mask = (1ll << n) - 1;
  68.     vector<int> path;
  69.     while (mask > 1) {
  70.         for (int i = 0; i < n; ++i) {
  71.             if ((mask >> i) & 1) {
  72.                 int maskwithouti = mask ^ (1ll << i);
  73.                 msk t1 = { dp[maskwithouti].cnt, dp[maskwithouti].maxw - cost[i] };
  74.                 msk t2 = { dp[maskwithouti].cnt + 1, S - cost[i] };
  75.                 if (dp[mask] == t1 || dp[mask] == t2) {
  76.                     path.push_back(i + 1);
  77.                     mask = maskwithouti;
  78.                     break;
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     //reverse(all(path));
  84.     vector<vector<int>> ans;
  85.     int curS = S;
  86.     vector<int> tmp;
  87.     for (int i = len(path) - 1; i >= 0; --i) {
  88.         if (curS < cost[path[i] - 1]) {
  89.             ans.push_back(tmp);
  90.             tmp.clear();
  91.             curS = S;
  92.         }
  93.         curS -= cost[path[i] - 1];
  94.         tmp.push_back(path[i]);
  95.     }
  96.     if (len(tmp) > 0)
  97.         ans.push_back(tmp);
  98.  
  99.     cout << len(ans) << endl;
  100.     for (int i = 0; i < len(ans); ++i) {
  101.         cout << len(ans[i]) << " ";
  102.         for (auto el : ans[i])
  103.             cout << el << " ";
  104.         cout << endl;
  105.     }
  106. }
  107.  
  108. signed main() {
  109.     ios::sync_with_stdio(0);
  110.     cin.tie(0);
  111.     cout.tie(0);
  112.  
  113.     int t; cin >> t;
  114.     while (t--) {
  115.         solve();
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment