#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
template<class InputIterator>
long long int sum(InputIterator first, InputIterator last){
long long int s=0;
while (first!=last) {
s+= *first;
++first;
}
return s;
}
int rek(vector<int> &plecaki, list<int> przedmioty, int ile_wzialem, vector<int>::iterator plecak, int masa, long long int sum_waga){
list<int>::iterator przedmiot;
//cout << "hello" << endl;
int minr = 1000;
for(plecak; plecak!=plecaki.end(); plecak++){
if(*(przedmioty.begin()) > *plecak || sum(plecak, plecaki.end()) < sum_waga)
break;
for(przedmiot=przedmioty.begin(); przedmiot!=przedmioty.end(); przedmiot++){
if(masa + *przedmiot <= *plecak){
list<int> przedmioty_tmp = przedmioty;
//
przedmioty_tmp.erase(find(przedmioty_tmp.begin(), przedmioty_tmp.end(), *przedmiot));
minr = min(rek(plecaki, przedmioty_tmp, ile_wzialem, plecak, masa + *przedmiot, sum_waga - *przedmiot), minr);
}
}
masa = 0;
if(przedmioty.size())
ile_wzialem++;
else
break;
}
if(przedmioty.size())
return min(1000, minr);
return ile_wzialem;
}
int main(){
ios_base::sync_with_stdio(0);
int n, m, tmp, out, max_w, max_p;
cin >> n >> m;
max_w = max_p = 0;
list<int> przedmioty;
vector<int> plecaki;
while(n--){
cin >> tmp;
max_w = max(tmp, max_w);
przedmioty.push_back(tmp);
}
while(m--){
cin >> tmp;
max_p = max(tmp, max_p);
plecaki.push_back(tmp);
}
long long int sum_waga = 0;
long long int sum_pojemnosc = 0;
for(list<int>::iterator przedmiot=przedmioty.begin(); przedmiot!=przedmioty.end(); przedmiot++)
sum_waga += *przedmiot;
for(vector<int>::iterator plecak=plecaki.begin(); plecak!=plecaki.end(); plecak++)
sum_pojemnosc += *plecak;
if(sum_waga > sum_pojemnosc || max_p < max_w){
cout << "NIE" << endl;
return 0;
}
przedmioty.sort();
przedmioty.reverse();
sort(plecaki.begin(), plecaki.end());
reverse(plecaki.begin(), plecaki.end());
int ret = rek(plecaki, przedmioty, 0, plecaki.begin(), 0, sum_waga);
if(ret == 1000)
cout << "NIE" << endl;
else
cout << ret + 1 << endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <iostream> #include <algorithm> #include <vector> #include <list> using namespace std; template<class InputIterator> long long int sum(InputIterator first, InputIterator last){ long long int s=0; while (first!=last) { s+= *first; ++first; } return s; } int rek(vector<int> &plecaki, list<int> przedmioty, int ile_wzialem, vector<int>::iterator plecak, int masa, long long int sum_waga){ list<int>::iterator przedmiot; //cout << "hello" << endl; int minr = 1000; for(plecak; plecak!=plecaki.end(); plecak++){ if(*(przedmioty.begin()) > *plecak || sum(plecak, plecaki.end()) < sum_waga) break; for(przedmiot=przedmioty.begin(); przedmiot!=przedmioty.end(); przedmiot++){ if(masa + *przedmiot <= *plecak){ list<int> przedmioty_tmp = przedmioty; // przedmioty_tmp.erase(find(przedmioty_tmp.begin(), przedmioty_tmp.end(), *przedmiot)); minr = min(rek(plecaki, przedmioty_tmp, ile_wzialem, plecak, masa + *przedmiot, sum_waga - *przedmiot), minr); } } masa = 0; if(przedmioty.size()) ile_wzialem++; else break; } if(przedmioty.size()) return min(1000, minr); return ile_wzialem; } int main(){ ios_base::sync_with_stdio(0); int n, m, tmp, out, max_w, max_p; cin >> n >> m; max_w = max_p = 0; list<int> przedmioty; vector<int> plecaki; while(n--){ cin >> tmp; max_w = max(tmp, max_w); przedmioty.push_back(tmp); } while(m--){ cin >> tmp; max_p = max(tmp, max_p); plecaki.push_back(tmp); } long long int sum_waga = 0; long long int sum_pojemnosc = 0; for(list<int>::iterator przedmiot=przedmioty.begin(); przedmiot!=przedmioty.end(); przedmiot++) sum_waga += *przedmiot; for(vector<int>::iterator plecak=plecaki.begin(); plecak!=plecaki.end(); plecak++) sum_pojemnosc += *plecak; if(sum_waga > sum_pojemnosc || max_p < max_w){ cout << "NIE" << endl; return 0; } przedmioty.sort(); przedmioty.reverse(); sort(plecaki.begin(), plecaki.end()); reverse(plecaki.begin(), plecaki.end()); int ret = rek(plecaki, przedmioty, 0, plecaki.begin(), 0, sum_waga); if(ret == 1000) cout << "NIE" << endl; else cout << ret + 1 << endl; return 0; } |
English