# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
428956 | LouayFarah | Detecting Molecules (IOI16_molecules) | C++14 | 0 ms | 0 KiB |
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;
#define pb push_back
int n, l, u;
vector<int> w;
vector<int> backtrack;
vector<int> pre;
bool solve(int i, int curr)
{
if(curr>=l&&curr<=u)
return true;
if(i==n)
return false;
if(curr>u)
return false;
int temp = 0;
if(i==0)
temp = curr + pre[n-1];
else
temp = curr + pre[n-1]-pre[i-1];
if(temp<l)
return false;
bool flag = false;
for(int j = i; j<n; j++)
{
backtrack.pb(j);
flag = solve(j+1, curr+w[j]);
if(flag)
break;
backtrack.pop_back();
}
return flag;
}
vector<int> find_subset(int L, int U, vector<int> W)
{
n = (int)W.size();
l = L;
u = U;
w = W;
sort(all(w), greater<int>());
pre.assign(n, 0);
pre[0] = w[0];
for(int i = 1; i<n; i++)
pre[i] = pre[i-1] + w[i];
bool flag = solve(0, 0);
if(flag)
{
vector<int> result = backtrack;
return result;
}
vector<int> result(0);
return result;
}