# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
350877 | spike1236 | Detecting Molecules (IOI16_molecules) | C++14 | 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 pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
veci find_subset(int asd, int asdas, veci w) {
ll l = asd, r = asdas;
int n = sz(w);
vector <pair <ll, int> > a;
for(int i = 0; i < n; ++i)
a.pb(mp(w[i], i));
sort(all(a));
ll sum = 0;
for(int i = n - 1; i >= 0; --i) {
sum += a[i].f;
if(a[n - 1].f - a[i].f <= r - l && l <= sum && sum <= r) {
veci res;
for(int j = i; j < n; ++j) res.pb(a[j].s);
return res;
}
}
multiset <pair <ll, int> > st;
sum = 0;
for(int i = 0; i < n; ++i) {
if(sum + a[i].f <= r) {
sum += a[i].f;
st.insert(a[i]);
} else {
if(!st.empty()) {
auto it = *st.rbegin();
st.erase(--st.end());
sum -= it.f;
sum += a[i].f;
st.insert(a[i]);
}
}
if(l <= sum && sum <= r) {
auto mx = *st.rbegin();
auto mn = *st.begin();
if(mx.f - mn.f <= r - l) {
veci res;
for(auto it : st) res.pb(it.s);
return res;
}
}
}
return {};
}
void solve() {
veci res = find_subset(10, 20, {15, 17, 16, 18});
cout << sz(res) << '\n';
for(int i = 0; i < sz(res); ++i) cout << res[i] << ' ';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
///cin >> T;
while(T--) solve(), cout << '\n';
return 0;
}