Submission #416881

#TimeUsernameProblemLanguageResultExecution timeMemory
416881snasibov05Detecting Molecules (IOI16_molecules)C++14
0 / 100
1093 ms204 KiB
#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; } i = u-1; } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...