题解 | #奥运排序问题#

奥运排序问题

https://www.nowcoder.com/practice/100a4376cafc439b86f5f8791fb461f3

复制结构体数组,之后四次排序,结果存原始数组中,最后算个最小值就可。

#include <bits/stdc++.h>
using namespace std;
int main() {
  struct rat {
    int id, gold, medal, people, rank[4];
  };
  int n, m;
  function<bool(rat, rat)> cmp[4] = {
      [](rat a, rat b) { return a.gold > b.gold; },
      [](rat a, rat b) { return a.medal > b.medal; },
      [](rat a, rat b) {
        return 1.0 * a.gold / a.people > 1.0 * b.gold / b.people;
      },
      [](rat a, rat b) {
        return 1.0 * a.medal / a.people > 1.0 * b.medal / b.people;
      }};
  while (cin >> n >> m) {
    vector<rat> nums(n);
    for (int i = 0; i < n; i++) {
      nums[i].id = i;
      cin >> nums[i].gold >> nums[i].medal >> nums[i].people;
    }
    vector<rat> tmp{nums};
    for (int i = 0; i < 4; i++) {
      sort(tmp.begin(), tmp.end(), cmp[i]);
      nums[tmp[0].id].rank[i] = 0;
      for (int j = 1; j < n; j++) {
        if (cmp[i](tmp[j - 1], tmp[j])) {
          nums[tmp[j].id].rank[i] = j;
        } else {
          nums[tmp[j].id].rank[i] = nums[tmp[j - 1].id].rank[i];
        }
      }
    }
    int x;
    for (int i = 0; i < m; i++) {
      cin >> x;
      auto mi = min_element(nums[i].rank, nums[i].rank + 4);
      cout << *mi + 1 << ":" << mi - nums[i].rank + 1 << endl;
    }
    cout << endl;
  }
  return 0;
}

全部评论

相关推荐

10-25 00:32
香梨想要offer:感觉考研以后好好学 后面能乱杀,目前这简历有点难
点赞 评论 收藏
分享
我见java多妩媚:大外包
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务