这个是我的代码, 也不知道怎么错. 同求高人指点.
#include <iostream> #include <algorithm> #include <set> #include <vector> #include <stack> #include <set> #include <list> #include <bitset> #include <cstring> using namespace std; struct Bear { int fight; int hungry;
}; int find_h(vector<Bear>& bears) { int i = 0; int h = -1; int idx = -1; while (i < bears.size()) { if (bears[i].hungry <= 0) {
i++; continue;
} if (h < bears[i].hungry) {
h = bears[i].hungry;
idx = i;
}
i++;
} return idx;
} void bear_eat(vector<Bear> &bears, int idx, vector<int>& suguars) { for (int i = suguars.size() - 1; i >= 0; i--) { if (suguars[i] == -1) continue; else if( suguars[i] > bears[idx].hungry) continue; else {
bears[idx].hungry -= suguars[i];
suguars[i] = -1; break;
}
}
} bool can_eat(vector<Bear> &bears, vector<int> &suguars) { vector<int> hs; for (int i = 0; i < bears.size(); i++) { if (bears[i].hungry > 0) {
hs.push_back(bears[i].hungry);
}
} for (int i = 0; i < suguars.size(); i++) { if (suguars[i] > 0) for (auto& h: hs) if (suguars[i] <= h) return true;
} return false;
} void hdl(vector<Bear>& bears, vector<int>& suguars) {
sort(suguars.begin(), suguars.end()); while (can_eat(bears, suguars)) { int most_h = find_h(bears); if (most_h == -1) break;
bear_eat(bears, most_h, suguars);
}
} int main() { int n, m;
cin >> n >> m; vector<int> suguars; for (int i = 0; i < m; i++) { int tmp;
cin >> tmp;
suguars.push_back(tmp);
} vector<Bear> bears; for (int i = 0; i < n; i++) { int f = 0, h = 0;
cin >> f >> h;
bears.push_back({f, h});
}
hdl(bears, suguars); for (int i = 0; i < bears.size() - 1; i++)
cout << bears[i].hungry << endl;
cout << bears[bears.size() - 1].hungry; return 0;
}