这个是我的代码, 也不知道怎么错. 同求高人指点. #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; }
点赞 评论

相关推荐

09-27 00:29
东北大学 Java
伟大的麻辣烫:查看图片
阿里巴巴稳定性 75人发布 投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
10-24 11:10
山西大学 Java
若梦难了:哥们,面试挂是很正常的。我大中厂终面挂,加起来快10次了,继续努力吧。
点赞 评论 收藏
分享
牛客网
牛客企业服务