美文网首页程序员
PAT A1012 The Best Rank

PAT A1012 The Best Rank

作者: Polylanger | 来源:发表于2021-01-23 12:55 被阅读0次
#include <cmath>
#include <vector>
#include <climits>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

class Grade {
public:
  string ID;
  int course[4];
  int rank[4];

  int bestRank = INT_MAX;
  char bestCourse;

  static int flag;
};

int Grade::flag = -1;

bool compare(Grade a, Grade b) { return a.course[Grade::flag] > b.course[Grade::flag]; }

int main() {

  const char COURSE[5] = "ACME";

  int N, M;
  cin >> N >> M;

  map<string, Grade> rankMap;
  vector<Grade> orderList;
  for (int i = 0; i < N; i++) {
    Grade grade;
    cin >> grade.ID >> grade.course[1] >> grade.course[2] >> grade.course[3];
    grade.course[0] = round((grade.course[1] + grade.course[2] + grade.course[3]) / 3.0);
    rankMap[grade.ID] = grade;
    orderList.push_back(grade);
  }

  vector<string> checklist;
  for (int i = 0; i < M; i++) {
    string ID;
    cin >> ID;
    checklist.push_back(ID);
  }

  for (int flag = 0; flag < 4; flag++) {
    Grade::flag = flag;
    sort(orderList.begin(), orderList.end(), compare);

    for (int i = 0; i < orderList.size(); i++) {
      string id = orderList.at(i).ID;
      rankMap[id].rank[Grade::flag] = i + 1;

      // 排名并列应该1、1、3、4、5,而不是1、1、2、3、4
      if (i != 0) {
        string lastID = orderList.at(i - 1).ID;
        if (rankMap[id].course[Grade::flag] == rankMap[lastID].course[Grade::flag]) {
          rankMap[id].rank[Grade::flag] = rankMap[lastID].rank[Grade::flag];
        }
      }
      
      if (rankMap.count(id) && rankMap[id].rank[Grade::flag] < rankMap[id].bestRank) {
        rankMap[id].bestRank = rankMap[id].rank[Grade::flag];
        rankMap[id].bestCourse = COURSE[flag];
      }
    }
  }

  for (int i = 0; i < checklist.size(); i++) {
    if (rankMap.count(checklist[i])) {
      cout << rankMap[checklist[i]].bestRank << " " << rankMap[checklist[i]].bestCourse << endl;
    }
    else {
      cout << "N/A" << endl;
    }
  }

  return 0;
}

相关文章

网友评论

    本文标题:PAT A1012 The Best Rank

    本文链接:https://www.haomeiwen.com/subject/cazyzktx.html