This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "molecules.h"
using namespace std;
struct node {
int idx, at, weight;
node () {}
node(int _at, int _weight, int _idx){
idx = _idx;
at = _at;
weight = _weight;
}
bool operator != (const node &tmp) {
if(idx == tmp.idx and at == tmp.at and weight == tmp.weight) {
return false;
}
return true;
}
};
int dp[10005][1005];
node backtrac[10005][1005];
vector<int> v;
int rec(int at, int weight) {
if(weight == 0) {
backtrac[at][weight] = node(-1, -1, -1);
return 1;
}
if(at == -1) {
return 0;
}
if(dp[at][weight] != -1) {
return dp[at][weight];
}
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] = node(at - 1, weight - v[at], at);
}
else {
backtrac[at][weight] = node(at - 1, weight, -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 {};
}
node at;
vector<pair<int, int> > res;
int tmp = 0;
vector<int> R;
// cout << backtrac[n - 1][W].at << " " << backtrac[n - 1][W].weight << endl;
for(at = node(n - 1, W, n - 1); at.at != -1; at = backtrac[backtrac[at.at][at.weight].at][backtrac[at.at][at.weight].weight]) {
//cout << at.at << " " << at.weight << " " << at.idx << endl;
if(at.idx != -1) {
R.push_back(at.idx + 1);
// cout << at.idx + 1 << endl;
}
}
return R;
}
Compilation message (stderr)
molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:65:9: warning: unused variable 'tmp' [-Wunused-variable]
65 | int tmp = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |