Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <map>
- #include <cstring>
- #include <queue>
- #include <algorithm>
- //#include <bits/stdc++.h>
- //#include "molecules.h"
- using namespace std;
- struct node {
- int idx, at, weight;
- node(int _idx, int _at, int _weight){
- idx = _idx;
- at = _at;
- weight = _weight;
- }
- };
- int dp[10005][1005];
- node backtrac[10005][1005];
- vector<int> v;
- int rec(int at, int weight) {
- if(weight == 0) {
- backtrac[at][weight].at = -1;
- backtrac[at][weight].weight = -1;
- backtrac[at][weight].idx = -1;
- return 1;
- }
- if(at == -1) {
- return 0;
- }
- if(dp[at][weight] != -1) {
- return dp[at][weight];
- }
- int result = 0;
- int r1 = 0, r2 = 0;
- if(weight - v[at] >= 0) {
- r1 = rec(at - 1, weight - v[at]);
- }
- r2 = rec(at - 1, weight);
- if(r1 > r2) {
- backtrac[at][weight].idx = at;
- backtrac[at][weight].weight = weight - v[at];
- backtrac[at][weight].at = at - 1;
- }
- else {
- backtrac[at][weight].idx = -1;
- backtrac[at][weight].weight = weight - v[at];
- backtrac[at][weight].at = at - 1;
- }
- return dp[at][weight] = max(r1, r2);
- }
- vector<int> find_subset(int l, int u, vector<int> w) {
- v =w;
- int n = w.size();
- int W = -1;
- memset(dp, -1, sizeof dp);
- for(int i = l; i <= u; i++) {
- if(rec(n - 1, i) == 1) {
- W = i;
- break;
- }
- }
- if(W == -1) {
- return {};
- }
- pair<int, int> at;
- vector<pair<int, int> > res;
- int tmp = 0;
- 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]) {
- res.push_back(make_pair(at.first, at.second));
- }
- vector<int> R;
- for(int i = 0; i< (int) res.size(); i++) {
- if(i + 1 < (int) res.size() and res[i].second != res[i + 1].second) {
- R.push_back(res[i].first + 1);
- cout << res[i].first + 1 << endl;
- }
- }
- return R;
- }
- int main() {
- ios::sync_with_stdio(0);
- vector<int> v = find_subset(10, 20, {15, 17, 16, 18});
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement