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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cstring>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <random>
#define FOR(var,bound) for(int var = 0; var < bound; var++)
#define FORB(var,lb,ub) for (int var = lb; var < ub; var++)
#define FORR(var,bound) for(int var = bound-1; var >= 0; var--)
#define int long long
//#define RANDOMISE
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
vector<signed> find_subset(signed l, signed u, vector<signed> w) {
vector<int> sortedPerm(w.size());
FOR(i, w.size()) sortedPerm[i] = i;
sort(sortedPerm.begin(), sortedPerm.end(), [&](int a, int b) {
return w[a] < w[b];
});
int sum = 0;
int li = 0;
int ri = -1;
while (ri < (int) w.size() && li < w.size()) {
if (l <= sum && sum <= u) {
vector<signed> res;
for (int i = li ; i <= ri; i++) {
res.push_back(sortedPerm[i]);
}
return res;
} else if (sum < l) {
if (ri + 1 < (int) w.size()) {
sum += w[sortedPerm[++ri]];
} else break;
} else {
sum -= w[sortedPerm[li++]];
}
}
return {};
}
#ifdef LOCAL_TEST
signed main() {
#ifdef RANDOMISE
FOR(iter, 10000) {
#endif
signed n, l, u;
#ifdef RANDOMISE
n = 100;
l = rand() % 1000 + 1;
if (l == 1000) {
u = 1000;
} else {
u = l + (rand() % (1001 - l));
}
vector<signed> w(n);
fill(w.begin(), w.end(), rand() % 100 + 1);
#else
assert(3 == scanf("%d %d %d", &n, &l, &u));
std::vector<signed> w(n);
for (int i = 0; i < n; i++)
assert(1 == scanf("%d", &w[i]));
#endif
std::vector<signed> result = find_subset(l, u, w);
#ifdef RANDOMISE
if (result.size() == 0) {
int count = (l + w[0] - 1) / w[0];
int mres = w[0] * count;
if (mres >= l && mres <= u && count <= n) {
cout << "Didn't find solution!" << endl;
cout << "w = " << w[0] << " range = [" << l << ", " << u << "]" << endl;
exit(0);
}
} else {
int res = 0;
for (int i: result) {
res += w[i];
}
if (res < l) {
cout << "Too small!" << endl;
exit(0);
}
if (res > u) {
cout << "Too big!" << endl;
exit(0);
}
set<int> unique(result.begin(), result.end());
if (unique.size() != result.size()) {
cout << "Duplicate molecules!" << endl;
cout << "w = " << w[0] << " range = [" << l << ", " << u << "]" << endl;
exit(0);
}
}
}
#else
printf("%d\n", (signed) result.size());
for (int i = 0; i < (int)result.size(); i++)
printf("%d%c", result[i], " \n"[i == (signed)result.size() - 1]);
#endif
}
#endif
Compilation message (stderr)
molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:15:45: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | #define FOR(var,bound) for(int var = 0; var < bound; var++)
......
31 | FOR(i, w.size()) sortedPerm[i] = i;
| ~~~~~~~~~~~
molecules.cpp:31:5: note: in expansion of macro 'FOR'
31 | FOR(i, w.size()) sortedPerm[i] = i;
| ^~~
molecules.cpp:41:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while (ri < (int) w.size() && li < w.size()) {
| ~~~^~~~~~~~~~
# | 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... |