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 "molecules.h"
#include <map>
#include <deque>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<int,int> pii;
/*
4 15 17
6 8 8 7
=>2
=>2 3
4 14 15
5 5 6 6
=>0
4 10 20
15 17 16 18
=>1
=>3
4 0 0
0 0 0 0
=>1
=>3
*/
vector<int> find_subset(int l, int u, vector<int> w) {
const int& N = w.size();
vector<pii> wVtr(N);
for(int i = 0; i < N; ++i)
wVtr[i] = pii(w[i], i);
sort(wVtr.begin(), wVtr.end());
while(!wVtr.empty() && wVtr.front().first > u) wVtr.pop_back();
if(wVtr.empty()) return vector<int>(0);
if(l <= wVtr.back().first && wVtr.back().first <= u) return vector<int>(1,wVtr.back().second);
reverse(wVtr.begin(),wVtr.end());
// cout << "w:";
// for(const auto& i:w) cout << i << " ";
// cout << endl;
int UpperBound = u;
vector<int> LowerBound(N);
LowerBound[N-1] = l-wVtr[N-1].first;
for(int i = N-2; i >= 0; --i)
LowerBound[i] = max(LowerBound[i+1] - wVtr[i].first, 0);
// cout << "LowerBound[]:";
// for(const auto& i:LowerBound) cout << i << " ";
// cout << endl;
vector<pair<bool,deque<int>>> PossibleNumbers(UpperBound+1);
// map<int,deque<int>> PossibleNumbers;
PossibleNumbers[0] = pair<bool,deque<int>>(true, deque<int>());
// PossibleNumbers[0] = deque<int>();
deque<pair<int,deque<int>>> ToInsert;
for(int i = 0; i < N; ++i){
const pii& w_i = wVtr[i];
for(int j = LowerBound[i]; j <= UpperBound; ++j){
const auto& n = PossibleNumbers[j];
if(n.first && j+w_i.first <= UpperBound){
ToInsert.push_back(pair<int,deque<int>>(j+w_i.first, n.second));
ToInsert.back().second.push_back(w_i.second);
}
}
while(!ToInsert.empty()){
PossibleNumbers[ToInsert.back().first] = pair<bool,deque<int>>(true,ToInsert.back().second);
ToInsert.pop_back();
}
// cout << "PossibleNumbers (before purge):" << endl;
// for(const auto& n:PossibleNumbers){
// cout << n.first << ": ";
// for(const auto& m:n.second) cout << m << " ";
// cout << endl;
// }
// while(!PossibleNumbers.empty() && PossibleNumbers.begin()->first < LowerBound_i)
// PossibleNumbers.erase(PossibleNumbers.begin());
// cout << "PossibleNumbers:" << endl;
// for(int k = 0; k < PossibleNumbers.size(); ++k){
// cout << k << ": " << (PossibleNumbers[k].first? "TRUE" : "FALSE") << " ";
// for(const auto& m:PossibleNumbers[k].second) cout << m << " ";
// cout << endl;
// }cout << endl;
}
for(int i = l; i <= u; ++i){
if(PossibleNumbers[i].first)
return vector<int>(PossibleNumbers[i].second.begin(), PossibleNumbers[i].second.end());
}
return vector<int>(0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |