// 1012. The Best Rank.cpp: 主项目文件。
#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::sort;
const int N=10003;
typedef struct Stu{
char id[8];
int A,C,M,E;
int rank[4];
}Stu;
Stu stu[N];
int n,version;
bool cmp(Stu m1,Stu m2){
if(version==0)
return m1.A>m2.A;
else if(version==1)
return m1.C>m2.C;
else if(version==2)
return m1.M>m2.M;
else
return m1.E>m2.E;
}
bool cmp1(Stu m1,Stu m2){
return strcmp(m1.id,m2.id)<0;
}
void getRank(){
sort(stu,stu+n,cmp);
stu[0].rank[version]=1;
for(int i=1;i<n;i++){
if(version==0){
if(stu[i].A==stu[i-1].A)
stu[i].rank[version]=stu[i-1].rank[version];
else
stu[i].rank[version]=i+1;
}
else if(version==1){
if(stu[i].C==stu[i-1].C)
stu[i].rank[version]=stu[i-1].rank[version];
else
stu[i].rank[version]=i+1;
}
else if(version==2){
if(stu[i].M==stu[i-1].M)
stu[i].rank[version]=stu[i-1].rank[version];
else
stu[i].rank[version]=i+1;
}
else{
if(stu[i].E==stu[i-1].E)
stu[i].rank[version]=stu[i-1].rank[version];
else
stu[i].rank[version]=i+1;
}
}
}
int binarySearch(char *query,int low,int high){
if(low<=high){
int mid=(low+high)/2;
if(strcmp(query,stu[mid].id)==0)
return mid;
else if(strcmp(query,stu[mid].id)>0)
return binarySearch(query,mid+1,high);
else
return binarySearch(query,low,mid-1);
}
return -1;
}
void getBestRank(int pos){
int min=stu[pos].rank[0],minf=0;
for(int i=1;i<4;i++){
if(stu[pos].rank[i]<min)
min=stu[pos].rank[i],minf=i;
}
printf("%d ",min);
if(minf==0)
printf("A\n");
else if(minf==1)
printf("C\n");
else if(minf==2)
printf("M\n");
else
printf("E\n");
}
int main()
{
int m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
double a,c,m,e;
scanf("%s%lf%lf%lf",stu[i].id,&c,&m,&e);
stu[i].C=(int)c,stu[i].M=(int)m,stu[i].E=(int)e;
stu[i].A=(int)((c+m+e)/3+0.5);//四舍五入
}
for(int i=0;i<4;i++){
version=i;
getRank();
}
sort(stu,stu+n,cmp1);
while(m--){
char query[8];
scanf("%s",query);
int index=binarySearch(query,0,n-1);
if(index==-1)
puts("N/A");
else
getBestRank(index);
}
return 0;
}
1012. The Best Rank
最新推荐文章于 2022-03-02 19:29:24 发布