# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
392421 | usachevd0 | Detecting Molecules (IOI16_molecules) | C++17 | 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>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
return y > x ? (x = y, true) : false;
}
void debug_out() {
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
cerr << ' ' << A;
debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
for (int i = 0; i < n; ++i) {
cerr << a[i] << ' ';
}
cerr << endl;
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& a) {
for (auto& x : a) {
stream << x << ' ';
}
return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
return stream << p.first << ' ' << p.second;
}
#define int ll
vector<int> find_subset(int L, int R, vector<int> w) {
int n = w.size();
sort(all(w));
if (accumulate(all(w), 0LL) < L) {
return {};
}
if (accumulate(all(w), 0LL) <= R) {
vector<int> ans(n);
iota(all(ans), 0);
return ans;
}
if (n <= 1) {
return {};
}
int p = n;
ll suff = 0;
ll pref = accumulate(w.begin(), w.end() - 1, 0LL);
for (int i = n - 2; i >= -1; --i) {
while (p - 1 > i && suff + w[p - 1] + pref <= R) {
suff += w[--p];
}
debug(i, p, pref, suff);
if (pref + suff >= L && pref + suff <= R) {
vector<int> ans;
for (int j = 0; j <= i; ++j)
ans.pb(j);
for (int j = p; j < n; ++j)
ans.pb(j);
return ans;
}
if (i >= 0) pref -= w[i];
}
return {};
}
#ifdef DEBUG
int32_t main() {
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int n, L, R;
cin >> n >> L >> R;
vector<int> w(n);
for (int i = 0; i < n; ++i) cin >> w[i];
vector<int> ans = find_subset(L, R, w);
int m = ans.size();
debug(m);
debug(ans);
ll sum = 0;
for (int i = 0; i < m; ++i)
sum += w[ans[i]];
assert(sum == 0 || L <= sum && sum <= R);
return 0;
}
#endif