Submission #1008898

#TimeUsernameProblemLanguageResultExecution timeMemory
1008898jer033Detecting Molecules (IOI16_molecules)C++17
100 / 100
39 ms10368 KiB
#include "molecules.h" #include <bits/stdc++.h> using namespace std; using ll = long long; using pli = pair<long long, int>; std::vector<int> find_subset(int l, int u, std::vector<int> w) { int molecule_count = w.size(); vector<pli> molecules; molecules.reserve(molecule_count); for (int i=0; i<molecule_count; i++) { molecules.push_back({w[i], i}); } sort(molecules.begin(), molecules.end()); ll lower_bound = l; ll upper_bound = u; vector<ll> prefix_sums; vector<ll> suffix_sums; prefix_sums.reserve(molecule_count+1); suffix_sums.reserve(molecule_count+1); prefix_sums.push_back(0); for (int i=0; i<molecule_count; i++) { prefix_sums.push_back(prefix_sums[i]+molecules[i].first); } suffix_sums.push_back(0); for (int i=0; i<molecule_count; i++) { suffix_sums.push_back(suffix_sums[i]+molecules[molecule_count-1-i].first); } int ele_count = -1; for (int i=0; i<=molecule_count; i++) if ((prefix_sums[i]<=u) and (suffix_sums[i]>=l)) ele_count = i; if (ele_count == -1) return {}; vector<int> rearranged_indices; rearranged_indices.reserve(ele_count+1); for (int i=0; i<ele_count; i++) rearranged_indices.push_back(i); ll curr_sum = prefix_sums[ele_count]; int j = 0; while (curr_sum < l) { rearranged_indices[j] = molecule_count-1-j; curr_sum = curr_sum - molecules[j].first + molecules[molecule_count-1-j].first; j++; } vector<int> ans; ans.reserve(ele_count+1); for (int i=0; i<ele_count; i++) { ans.push_back(molecules[rearranged_indices[i]].second); } return ans; }

Compilation message (stderr)

molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:16:8: warning: unused variable 'lower_bound' [-Wunused-variable]
   16 |     ll lower_bound = l;
      |        ^~~~~~~~~~~
molecules.cpp:17:8: warning: unused variable 'upper_bound' [-Wunused-variable]
   17 |     ll upper_bound = u;
      |        ^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...