# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1268807 | bgnbvnbv | Detecting Molecules (IOI16_molecules) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
vector<int> find_subset(int l, int u, const vector<int> &w) {
int n = w.size();
vector<pair<int,int>> a(n);
for(int i = 0; i < n; i++) a[i] = {w[i], i};
sort(a.begin(), a.end());
vector<long long> pref(n+1, 0);
for(int i = 0; i < n; i++) pref[i+1] = pref[i] + a[i].first;
int j = 0;
for(int i = 1; i <= n; i++) {
// pref[i] - pref[j] là tổng đoạn [j..i-1]
while(j < i && pref[i] - pref[j] > u) j++;
if(j < i && pref[i] - pref[j] >= l && pref[i] - pref[j] <= u) {
vector<int> res;
for(int k = j; k < i; k++) res.push_back(a[k].second);
return res;
}
}
return {};
}
int main(){
int l = 15, u = 17;
vector<int> w = {6,8,8,7};
auto ans = find_subset(l,u,w);
if(ans.empty()) cout << "No subset\n";
else{
for(int x: ans) cout << x+1 << " "; // in 1-based
cout << "\n";
}
}