1012. The Best Rank (25)

来源:互联网 发布:seo黑帽技术教程 编辑:程序博客网 时间:2024/06/15 21:54

1012. The Best Rank (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A310101     98 85 88 90310102     70 95 88 84310103     82 87 94 88310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output "N/A".

Sample Input
5 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999
Sample Output
1 C1 M1 E1 A3 AN/A
类型:排序

题目大意:现已知n个考生的3门分数,平均分可以按照这三门算出来。然后分别对这四个分数从高到低排序,这样对每个考生来说有4个排名。k个查询,对于每一个学生id,输出当前id学生的最好的排名和它对应的分数,如果名次相同,按照A>C>M>E的顺序输出。如果当前id不存在,输出N/A
分析:
1.用结构体存储学生的id、四门成绩、四门排名、最好的排名的对应的科目下标
2.注意,排名并列应该1、1、3、4、5,而不是1、1、2、3、4,否则会有一个测试点不过
3.注意,平均分是四舍五入的,所以需要按照+0.5后取整,保证是四舍五入的(听说不四舍五入也能通过…)
4.存储的时候就按照ACME的顺序存储可以简化程序逻辑
5.用exist数组保存当前id是否存在,这个id对应的stu结构体的下标是多少。用i+1可以保证为0的都是不存在的可以直接输出N/A,其余不为0的保存的值是对应的结构体index + 1的值

#include<iostream>#include<algorithm>using namespace std;struct node{int id,best;int score[4];//四门课成绩 int rank[4];//四门课排名 }stu[2005];int n,m;int exist[1000000];//学号存在的标志 int flag=-1;char ch[5]="ACME";int cmp(const node &a,const node &b)//用全局变量flag来进行表示,可以减少四个比较函数的书写 {return a.score[flag]>b.score[flag];}int main(){//freopen("in.txt","r",stdin);cin>>n>>m;int id;for(int i=0;i<n;++i){cin>>stu[i].id>>stu[i].score[1]>>stu[i].score[2]>>stu[i].score[3];stu[i].score[0]=(stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/3.0+0.5;}for(flag=0;flag<4;++flag)//统计各科排名 {sort(stu,stu+n,cmp);stu[0].rank[flag]=1;for(int i=1;i<n;++i){stu[i].rank[flag]=i+1;//进行各科排名,并列排名应该占据名额。如1、1、3、4、5而不是1、1、2、3、4 if(stu[i].score[flag]==stu[i-1].score[flag])stu[i].rank[flag]=stu[i-1].rank[flag];}}for(int i=0;i<n;++i)//统计找出由此按顺序靠前的排名 {exist[stu[i].id]=i+1;//这一步很关键,记录下学号在数组中的位置,可以达到随机访问 stu[i].best=0;int minrank=stu[i].rank[0];for(int j=1;j<4;++j){if(stu[i].rank[j]<minrank){minrank=stu[i].rank[j];stu[i].best=j;}}} for(int i=0;i<m;++i){cin>>id;int temp=exist[id];if(temp>0){int best=stu[temp-1].best;cout<<stu[temp-1].rank[best]<<" "<<ch[best]<<endl;}elsecout<<"N/A"<<endl;}return 0;}

(2)粘上别人的

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;struct list{    int id;    int grade[4];}all[3000];int now;int rank_final[10000000][4]={0};char since[4]={'A','C','M','E'};int cmp(list a, list b){    return a.grade[now] > b.grade[now];}int main(){    int have,search;    scanf("%d%d",&have,&search);    for(int i=0;i<have;++i)    {        scanf("%d%d%d%d",&all[i].id,&all[i].grade[1],&all[i].grade[2],&all[i].grade[3]);        all[i].grade[0]=round((all[i].grade[1]+all[i].grade[2]+all[i].grade[3])/3.0)+0.5;    }    //竖的排序    for(now=0;now<4;++now)    {        sort(all,all+have,cmp);        rank_final[all[0].id][now]=1;        for(int i=1;i<have;++i)        {            if(all[i].grade[now]==all[i-1].grade[now])            {                rank_final[all[i].id][now]=rank_final[all[i-1].id][now];            }            else            {                rank_final[all[i].id][now]=i+1;            }        }    }    for(int i=0;i<search;++i)    {        int query;        scanf("%d",&query);        if(rank_final[query][0]==0)            printf("N/A\n");        else        {            int min_since=0;//寻找排名最高的科目            for(int j=1;j<4;++j)            {                if(rank_final[query][j] < rank_final[query][min_since])                    min_since=j;            }            printf("%d %c\n",rank_final[query][min_since],since[min_since]);        }    }}

(3)再来个复杂的,没有比较就没有伤害

//#include<string>  //#include <iomanip>  #include<vector>  #include <algorithm>  //#include<stack>  #include<set>  #include<queue>  #include<map>  //#include<unordered_set>  //#include<unordered_map>  //#include <sstream>  //#include "func.h"  //#include <list>  #include<stdio.h>  #include<iostream>  #include<string>  #include<memory.h>  #include<limits.h>  using namespace std;  struct student{      int c, m, e, rank;      int a;      string course, ID;      int tmpRank;//用来存在不同状态下的排名,主要是更新并列排名      student() :c(0), m(0), e(0), a(0), rank(9999999), tmpRank(9999999), course(""), ID(""){};  };  bool cmpC(const student&a, const student&b)  {      if (a.c > b.c) return true;      else return false;  }  bool cmpM(const student&a, const student&b)  {      if (a.m > b.m) return true;      else return false;  }  bool cmpE(const student&a, const student&b)  {      if (a.e > b.e) return true;      else return false;  }  bool cmpA(const student&a, const student&b)  {      if (a.a > b.a) return true;      else return false;  }  int main(void) {        int n, m;      cin >> n >> m;      vector<student> stu(n);      for (int i = 0; i < n; i++)      {          cin >> stu[i].ID >> stu[i].c >> stu[i].m >> stu[i].e;          stu[i].a = (stu[i].c + stu[i].m + stu[i].e);          stu[i].course = "";          stu[i].rank = 9999999;      }      //比较平均分      sort(stu.begin(), stu.end(), cmpA);      for (int i = 0; i < n; i++)      {          if (i == 0)              stu[i].tmpRank = 0;          else if (stu[i].a == stu[i - 1].a)              stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同          else              stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4          if (stu[i].rank > stu[i].tmpRank + 1)          {              stu[i].rank = stu[i].tmpRank + 1;              stu[i].course = "A";          }      }      //比较C语言      sort(stu.begin(), stu.end(), cmpC);      for (int i = 0; i < n; i++)      {          if (i == 0)              stu[i].tmpRank = 0;          else if (stu[i].c == stu[i - 1].c)              stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同          else              stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4          if (stu[i].rank > stu[i].tmpRank + 1)          {              stu[i].rank = stu[i].tmpRank + 1;              stu[i].course = "C";          }      }      //比较Mathematics      sort(stu.begin(), stu.end(), cmpM);      for (int i = 0; i < n; i++)      {          if (i == 0)              stu[i].tmpRank = 0;          else if (stu[i].m == stu[i - 1].m)              stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同          else              stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4          if (stu[i].rank > stu[i].tmpRank + 1)          {              stu[i].rank = stu[i].tmpRank + 1;              stu[i].course = "M";          }      }      //比较English      sort(stu.begin(), stu.end(), cmpE);      for (int i = 0; i < n; i++)      {          if (i == 0)              stu[i].tmpRank = 0;          else if (stu[i].e == stu[i - 1].e)              stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同          else              stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4          if (stu[i].rank > stu[i].tmpRank + 1)          {              stu[i].rank = stu[i].tmpRank + 1;              stu[i].course = "E";          }      }      map<string, student> ma;      for (int i = 0; i < n; i++)      {          ma[stu[i].ID] = stu[i];      }        for (int i = 0; i < m; i++)      {          string tmp;          cin >> tmp;          if (ma.find(tmp) == ma.end())              cout << "N/A" << endl;          else              cout << ma[tmp].rank << " " << ma[tmp].course << endl;      }      return 0;  }  


原创粉丝点击