Advertisement
Josif_tepe

Untitled

Apr 20th, 2023
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <map>
  5. #include <cstring>
  6. #include <queue>
  7. #include <algorithm>
  8. //#include <bits/stdc++.h>
  9. //#include "molecules.h"
  10. using namespace std;
  11.  
  12. struct node {
  13.     int idx, at, weight;
  14.     node(int _idx, int _at, int _weight){
  15.         idx = _idx;
  16.         at = _at;
  17.         weight = _weight;
  18.     }
  19. };
  20. int dp[10005][1005];
  21. node backtrac[10005][1005];
  22. vector<int> v;
  23. int rec(int at, int weight) {
  24.     if(weight == 0) {
  25.         backtrac[at][weight].at = -1;
  26.         backtrac[at][weight].weight = -1;
  27.         backtrac[at][weight].idx = -1;
  28.        
  29.         return 1;
  30.     }
  31.     if(at == -1) {
  32.         return 0;
  33.     }
  34.     if(dp[at][weight] != -1) {
  35.         return dp[at][weight];
  36.     }
  37.     int result = 0;
  38.     int r1 = 0, r2 = 0;
  39.     if(weight - v[at] >= 0) {
  40.         r1 = rec(at - 1, weight - v[at]);
  41.     }
  42.     r2 =  rec(at - 1, weight);
  43.    
  44.     if(r1 > r2) {
  45.         backtrac[at][weight].idx = at;
  46.         backtrac[at][weight].weight = weight - v[at];
  47.         backtrac[at][weight].at = at - 1;
  48.        
  49.        
  50.     }
  51.     else {
  52.         backtrac[at][weight].idx = -1;
  53.         backtrac[at][weight].weight = weight - v[at];
  54.         backtrac[at][weight].at = at - 1;
  55.        
  56.     }
  57.     return dp[at][weight] = max(r1, r2);
  58. }
  59. vector<int> find_subset(int l, int u, vector<int> w) {
  60.     v  =w;
  61.     int n = w.size();
  62.     int W = -1;
  63.     memset(dp, -1, sizeof dp);
  64.     for(int i = l; i <= u; i++) {
  65.         if(rec(n - 1, i) == 1) {
  66.             W = i;
  67.             break;
  68.         }
  69.     }
  70.     if(W == -1) {
  71.         return {};
  72.     }
  73.     pair<int, int> at;
  74.     vector<pair<int, int> > res;
  75.     int tmp = 0;
  76.     for(at = make_pair(n - 1, W); at != make_pair(-1, -1); at = backtrac[backtrac[at.first][at.second].first][backtrac[at.first][at.second].second]) {
  77.         res.push_back(make_pair(at.first, at.second));
  78.     }
  79.     vector<int> R;
  80.     for(int i = 0; i< (int) res.size(); i++) {
  81.         if(i + 1 < (int) res.size() and res[i].second != res[i + 1].second) {
  82.             R.push_back(res[i].first + 1);
  83.             cout << res[i].first + 1 << endl;
  84.         }
  85.     }
  86.     return R;
  87. }
  88.  
  89. int main() {
  90.     ios::sync_with_stdio(0);
  91.     vector<int> v = find_subset(10, 20, {15, 17, 16, 18});
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement