# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1187751 | dirtycodehehe | Detecting Molecules (IOI16_molecules) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
int find_subset(int l, int u, int w[], int n, int result[]) {
vector<pair<int, int>> arr(n); // pair<giá trị, chỉ số gốc>
for (int i = 0; i < n; ++i)
arr[i] = {w[i], i};
sort(arr.begin(), arr.end()); // sắp xếp tăng dần theo giá trị
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += arr[i].first;
if (sum >= l && sum <= u) {
// copy chỉ số gốc vào result
for (int j = 0; j <= i; ++j)
result[j] = arr[j].second;
return i + 1; // số lượng phần tử
}
}
return 0; // không tìm được tập con nào thỏa mãn
}