# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
711718 | JANCARAPAN | 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 int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(a) (long long) a.size()
#define endl '\n'
#define dbg(a) cerr << #a << " = " << a << endl;
#define print(a) for (auto x : a) cerr << x << " "; cerr << endl;
const long long INF = 1e18, MOD = 1e9+7;
vector<int> find_subset(int l, int r, vector<int> tmp) {
int n = sz(tmp);
vector<pair<int,int>> a(n);
for (int i = 0; i < n; i++) {
a[i] = {tmp[i], i};
}
sort(all(a));
vector<int> pref(n), suff(n);
for (int i = 0; i < n; i++) {
pref[i] = a[i].fi + (i ? pref[i - 1] : 0);
suff[n - i - 1] = a[n - i - 1].fi + (i ? suff[n - i] : 0);
}
vector<int> ans;
for (int size = 0; size < n; size++) {
if (pref[size] > r || suff[n - size - 1] < l) continue;
ll cur = pref[size];
int j = 0;
while (cur < l && j <= size) {
cur += a[n - j - 1].fi - a[j].fi;
j += 1;
}
if (cur >= l) {
assert(cur <= r);
for (int it = j; it <= size; it++) {
ans.pb(a[it].se);
}
for (int it = n - 1; it >= n - j; it--) {
ans.pb(a[it].se);
}
assert(sz(ans) > 0);
break;
}
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int l = 12, r = 12, n = 4;
cin >> l >> r >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
vector<int> a = find_subset(l, r, v);
for (auto x : a) cerr << x << " ";
}
//signed main() {
//ios_base::sync_with_stdio(0);
//cin.tie(0);
//int tt = 1;
//cin >> tt;
//while (tt--) {
//solve();
//}
//return 0;
//}