# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
411320 | hibye1217 | 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.
/// molecules.h ///
//#pragma once
#include <vector>
std::vector<int> find_subset(int l, int u, std::vector<int> w);
/// grader.cpp ///
#include <cstdio>
//#include <vector>
#include <cassert>
//#include "molecules.h"
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]);
}
/// molecules.cpp ///
//#include "molecules.h"
//#include <vector>
#include <algorithm>
#include <iostream>
using ll = long long;
std::vector<int> find_subset(int l, int u, std::vector<int> w) {
sort(w.begin(), w.end());
std::vector<int> ans;
int n = w.size();
ll pre = 0, suf = 0;
for (int i = 1; i <= n; i++){
pre += w[i-1]; suf += w[n-i];
if (pre > u){ return ans; }
if (l <= pre && pre <= u){
for (int j = 0; j < i; j++){ ans.push_back(j); }
return ans;
}
if (l <= suf && suf <= u){
for (int j = n-i; j < n; j++){ ans.push_back(j); }
return ans;
}
if (pre < l && u < suf){
ll sum = pre;
for (int j = 0; j+i < n; j++){
if (l <= sum && sum <= u){
for (int k = j; k <= j+i; k++){ ans.push_back(k); }
return ans;
}
sum -= w[j];
sum += w[j+i];
}
}
}
return ans;
}