#include <iostream> #include <vector> #include <algorithm> using namespace std; int findVal (vector < pair <long, int> > list, int x) { for (int i = 0; i < list.size(); i++){ if (list[i].first == x){ return i; } } return -1; } int main () { int n, m, t, a; cin >> n >> m; vector < pair <long, int> > obj; vector < long > plecak; for (int i = 0; i < n; i ++){ cin >> a; t = findVal(obj, a); if (t == -1){ obj.push_back(make_pair(a, 1)); } else { obj[t].second += 1; } } for (int i = 0; i < m; i ++){ cin >> a; plecak.push_back(a); } sort(obj.begin(), obj.end()); reverse(obj.begin(), obj.end()); sort(plecak.begin(), plecak.end()); while(plecak.size() > 0 && n > 0){ t = plecak.back(); plecak.erase(plecak.end() - 1, plecak.end()); for (int i = 0; i < obj.size(); i++){ if (t - obj[i].first >= 0 && obj[i].second > 0){ t -= obj[i].first; obj[i].second -= 1; if (obj[i].second == 0){ n--; } i--; } } } if (n == 0){ cout << m - plecak.size() << endl; } else { cout << "NIE" << 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int findVal (vector < pair <long, int> > list, int x) { for (int i = 0; i < list.size(); i++){ if (list[i].first == x){ return i; } } return -1; } int main () { int n, m, t, a; cin >> n >> m; vector < pair <long, int> > obj; vector < long > plecak; for (int i = 0; i < n; i ++){ cin >> a; t = findVal(obj, a); if (t == -1){ obj.push_back(make_pair(a, 1)); } else { obj[t].second += 1; } } for (int i = 0; i < m; i ++){ cin >> a; plecak.push_back(a); } sort(obj.begin(), obj.end()); reverse(obj.begin(), obj.end()); sort(plecak.begin(), plecak.end()); while(plecak.size() > 0 && n > 0){ t = plecak.back(); plecak.erase(plecak.end() - 1, plecak.end()); for (int i = 0; i < obj.size(); i++){ if (t - obj[i].first >= 0 && obj[i].second > 0){ t -= obj[i].first; obj[i].second -= 1; if (obj[i].second == 0){ n--; } i--; } } } if (n == 0){ cout << m - plecak.size() << endl; } else { cout << "NIE" << endl; } return 0; } |