이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "molecules.h"
#include <bits/stdc++.h>
using namespace std;
std::vector<int> find_subset(int l, int u, std::vector<int> w) {
srand(time(NULL));
long long n = (long long) w.size();
vector<bool> dp(u + 1);
dp[0] = true;
const long long mxxx = 10000;
if (n <= mxxx && u <= mxxx) {
vector<long long> par(u + 1);
vector<long long> idx(u + 1);
for (long long i = 0; i < n; i++) {
auto ndp = dp;
for (long long j = w[i]; j <= u; j++) {
if (!dp[j] && dp[j - w[i]]) {
ndp[j] = true;
idx[j] = i;
par[j] = j - w[i];
}
}
dp = ndp;
}
vector<int> res;
for (long long i = l; i <= u; i++) {
if (dp[i]) {
while (par[i]) {
res.push_back(idx[i]);
i = par[i];
}
res.push_back(idx[i]);
break;
}
}
return res;
} else {
vector<pair<long long, long long>> nums;
for (long long i = 0; i < n; i++) {
if (w[i] <= u) {
nums.emplace_back(w[i], i);
}
}
for (long long i = 0; i < 4; i++) {
if (i == 0)
sort(begin(nums), end(nums));
else if (i == 1)
sort(rbegin(nums), rend(nums));
else
random_shuffle(begin(nums), end(nums));
long long cur = 0;
multiset<pair<long long, long long>> ms;
for (auto& [x, y] : nums) {
cur += x;
ms.insert({x, y});
while (!ms.empty() && cur > u) {
long long remove = cur - u;
auto lb = ms.lower_bound({remove, -1});
if (lb == ms.end()) {
break;
}
cur -= ((*lb).first);
ms.erase(lb);
}
}
if (cur >= l && cur <= u) {
vector<int> res;
for (auto s : ms) {
res.push_back(s.second);
}
return res;
}
}
}
return {};
}
// long long main() {
// long long n, l, r;
// cin >> n >> l >> r;
// vector<long long> a(n);
// for (long long i = 0; i < n; i++) {
// cin >> a[i];
// }
// for (long long p : find_subset(l, r, a)) {
// cout << p << ' ';
// }
// }
# | 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... |