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 "molecules.h"
#include <algorithm>
using namespace std;
#define pb push_back
vector<vector<vector<bool>>> knapsack(vector<int>& w, int sum, vector<vector<vector<bool>>>& dp){
int n = w.size();
if (w[0] <= sum) dp[1][0][w[0]] = true;
dp[0][0][0] = true;
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j <= sum; ++j) {
if (dp[0][i][j] || dp[1][i][j]){
dp[0][i+1][j] = true;
if (j + w[i+1] <= sum) dp[1][i+1][j + w[i+1]] = true;
}
}
}
return dp;
}
vector<int> find_subset(int l, int u, vector<int> w) {
int n = w.size();
vector<vector<vector<bool>>> dp(2, vector<vector<bool>>(n, vector<bool>(u+1)));
knapsack(w, u, dp);
int res = -1;
for (int i = l; i <= u; ++i) {
if (dp[1][n-1][i] || dp[0][n-1][i]) {
res = i;
break;
}
}
vector<int> ans;
if (res == -1) return ans;
for (int i = n-1; i >= 0; --i){
if (dp[1][i][res]){
ans.pb(i);
res -= w[i];
}
}
reverse(ans.begin(), ans.end());
return ans;
}
# | 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... |