# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
875491 | AverageAmogusEnjoyer | 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;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,1:0; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,1:0; }
vector<int> find_subset(int l, int u, vector<int> w) {
int n = int(w.size());
vector<pair<ll,int>> v(n);
for (int i=0;i<n;i++) {
v[i]=make_pair(w[i],i);
}
sort(v.begin(),v.end());
vector<ll> p1(n),p2(n);
p1[0]=v[0].first;
p2[n-1]=v[n-1].first;
vector<int> res;
for (int i=1;i<n;i++) {
p1[i]=p1[i-1]+v[i].first;
}
for (int i=n-2;i>=0;--i) {
p2[i]=p2[i+1]+v[i].first;
}
for (int i=0;i<n;i++) {
if (p1[i]>=l&&p1[i]<=u) {
for (int k=0;k<=i;k++) {
res.push_back(v[k].second);
}
return res;
}
}
for (int i=0;i<n;i++) {
if (p2[i]>=l&&p2[i]<=u) {
for (int k=n-1;k>=i;k--) {
res.push_back(v[k].second);
}
return res;
}
}
for (int i=0;i<n;i++) {
int pos = lower_bound(p1.begin(),p1.end(),l-p2[i])-p1.begin();
if (pos==n||p2[i]+p1[pos]>u||pos>=i) {
continue;
}
for (int j=0;j<=pos;j++) {
res.push_back(v[j].second);
}
for (int j=i;j<n;j++) {
res.push_back(v[j].second);
}
return res;
}
return res;
}
int main() {
int l,u;
int n;
cin >> n >> l >> u;
vector<int> v(n);
for (int &x: v) {
cin >> x;
}
vector<int> r=find_subset(l,u,v);
for (int &x: r) {
cout << x << " " << v[x] << "\n";
}
}
/*
* Say I 'overbuilt' and got sum K > U for some prefix [0...i] and suffix [j...n-1]
* Now I want to 'add and subtract' some pair of numbers: that is, add a number which
* wasn't in previously and subtract an old one.
*/