PAT 甲级 1012 The Best Rank

    1. 输出格式:排名  科目代号(优先按照排
      #include <iostream>
      #include <algorithm>
      #include <vector>
      using namespace std;
      const int INF = 0x3fffffff;
      int n,f;
      
      struct student
      {
          int rank[4];
          int no;
          int flag;//
          int score[4];//to store C,M,E,A
      };
      
      vector<student> stu;
      
      bool cmp1(student a,student b)//compare students score
      {
          return a.score[f]>b.score[f];
      }
      
      bool cmp2(student a,student b)//compare students id
      {
          return a.no<b.no;
      }
      
      void ranking()
      {
          stu[0].rank[f]=1;
          for (int i = 1; i < n; ++i) {
              stu[i].rank[f]=i+1;//
              if(stu[i].score[f]==stu[i-1].score[f])
              {
                  stu[i].rank[f]=stu[i-1].rank[f];//since the priorities of the ranking methods are ordered as A,C,M,E
              }
          }
      }
      
      
      
      
      void judge(int id,int &rank,int &index)
      {
          rank=INF;
          index=0;
          for (int i = 0; i < 4; ++i) {
              if (stu[id].rank[i]<rank)
              {
                  rank=stu[id].rank[i];
                  index = i;
              }
      
          }
      }
      
      
      int search(int no)
      {
          int l=0,h=n-1;
          while(l<=h)
          {
              int mid = (l+h)/2;
              if(stu[mid].no==no)
              {
                  return mid;
              }
              else if(stu[mid].no<no)
              {
                  l=mid+1;
              }
              else
              {
                  h=mid-1;
      
              }
          }
          return -1;
      }
      int main() {
         // std::cout << "Hello, World!" << std::endl;
         int m,no;
         char s[4]={'A','C','M','E'};//等级数组
         scanf("%d %d",&n,&m);//n for data lines,m for ask student ID
      
          for (int i = 0; i < n; ++i) {
          student a;
          scanf("%d",&no);//no for student id;
          a.flag=1;
          a.no=no;
          scanf("%d %d %d",&a.score[1],&a.score[2],&a.score[3]);
          a.score[0]=(a.score[1]+a.score[2]+a.score[3])/3;
          stu.push_back(a);
      
          }
      
          for ( f = 0; f < 4; ++f) {
              sort(stu.begin(),stu.end(),cmp1);//descending order by score
              ranking();//just sort by rank key
          }
      
          sort(stu.begin(),stu.end(),cmp2);//ascending order by no
      
          for (int i = 0; i < m; ++i) {
              scanf("%d",&no);
              int rank = INF,index=0;
              int x=search(no);//bisection method find no
               if(x==-1)
               {
                   printf("N/A\n");
               }
               else
               {
                   judge(x,rank,index);//find the min rank and it's index
                   printf("%d %c\n",rank,s[index]);
               }
      
          }
      
          return 0;
      }
      View Code

       

      名靠前科目,最优科目若与他人同级,则按照ACME顺序)
posted @ 2020-11-15 15:25  ethon-wang  阅读(149)  评论(0编辑  收藏  举报