# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
770651 | Benmath | 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>
#include "molecules.h"
using namespace std;
std::vector<int> find_subset(int l, int u, std::vector<int> w) {
long long int sum=0;
vector<int>ans;
vector<pair<long long int,int> >arr;
int n=w.size();
for(int i=0;i<w.size();i++){
arr.push_back({w[i],i});
}
sort(arr.begin(),arr.end());
if(arr[0].first>u){
return ans;
}else if(arr[0].first>=l and arr[0].first<=u){
ans.push_back(arr[0].second);
return ans;
}else{
if(arr[n-1].first>=l and arr[n-1].first<=u){
ans.push_back(arr[n-1].second);
return ans;
}else{
int ti1=0;
int ti2=-1;
long long int sum=0;
for(int i=0;i<n;i++){
sum=sum+arr[i].first;
if(sum<l){
ti1=i;
}else if(sum>=l and sum<=u){
for(int j=0;j<=i;j++){
ans.push_back(arr[j].second);
}
return ans;
}else{
ti2=i;
break;
}
}
if(ti2==-1){
return ans;
}else{
long long int sum1=0;
for(int i=n-1;i>=(n-ti1-1);i--){
sum1=sum1+arr[i].first;
}
if(sum1<l){
return ans;
}
int j=n-1;
sum=sum-arr[ti2].first;
for(int i=ti1;i>=0;i--){
sum=sum+arr[j].first-arr[i].first;
if(sum>=l and sum<=u){
for(int k=0;k<i;k++){
ans.push_back(arr[k].second);
}
for(int k=j;k<=(n-1);k++){
ans.push_back(arr[k].second);
}
return ans;
}
j--;
}
}
}
}
for(int i=0;i<w.size();i++){
if(w[i]<=u){
sum=sum+w[i];
ans.push_back(i);
}
if(sum>=l and sum<=u){
break;
}
}
if(sum>=l and sum<=u){
return ans;
}else{
ans.clear();
return ans;}
}
/*
int main() {
int n, l, u;
assert(3 == scanf("%d %d %d", &n, &l, &u));
std::vector<int> w(n);
for (int i = 0; i < n; i++)
assert(1 == scanf("%d", &w[i]));
std::vector<int> result = find_subset(l, u, w);
printf("%d\n", (int)result.size());
for (int i = 0; i < (int)result.size(); i++)
printf("%d%c", result[i], " \n"[i == (int)result.size() - 1]);
}
*/