pat1012. The Best Rank (25)
1012. The Best Rank (25)
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 A 310101 98 85 88 90 310102 70 95 88 84 310103 82 87 94 88 310104 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 Input5 6 310101 98 85 88 310102 70 95 88 310103 82 87 94 310104 91 91 91 310105 85 90 90 310101 310102 310103 310104 310105 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A
注意点:
1.题目先给出n个学生的情况,先对这n个学生按照A、C、M、E的顺序依次进行排序,按照题意得到每个学生最好的排名情况,存储下来。然后,查询m个学生,如果学生不存在,则输出“N/A”,否则,输出最好排名及对应A、C、M、E的哪种情况。
2.题目中如果排名是1 2 2 4 5 6 6 8规则排名(见代码),却不是1 2 2 3 4 5 5 6
3.map的使用:
http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html
添加元素:
map<int ,string> maplive;
1)maplive.insert(pair<int,string>(102,"aclive"));
2)maplive.insert(map<int,string>::value_type(321,"hai"));
3)maplive[112]="April"; //map中最简单最常用的插入添加!
查找元素:
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else
cout<<"wo find 112"<<endl;
删除元素:
map<int ,string >::iterator l_it;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else maplive.erase(l_it); //删除元素
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <queue> 5 #include <stack> 6 #include <algorithm> 7 #include <map> 8 #include <iostream> 9 using namespace std; 10 struct cnode{ 11 int grade; 12 string id; 13 }; 14 struct mnode{ 15 int grade; 16 string id; 17 }; 18 struct enode{ 19 int grade; 20 string id; 21 }; 22 struct anode{ 23 int grade; 24 string id; 25 }; 26 struct somenode{ 27 int rank; 28 string r; 29 }; 30 map<string,somenode> some; 31 bool cmpa(anode a,anode b){ 32 return a.grade>b.grade; 33 } 34 bool cmpc(cnode a,cnode b){ 35 return a.grade>b.grade; 36 } 37 bool cmpm(mnode a,mnode b){ 38 return a.grade>b.grade; 39 } 40 bool cmpe(enode a,enode b){ 41 return a.grade>b.grade; 42 } 43 int main(){ 44 //freopen("D:\\INPUT.txt","r",stdin); 45 int n,m,cg,mg,eg; 46 string num; 47 scanf("%d %d",&n,&m); 48 anode *ap=new anode[n]; 49 cnode *cp=new cnode[n]; 50 mnode *mp=new mnode[n]; 51 enode *ep=new enode[n]; 52 int i; 53 for(i=0;i<n;i++){ 54 cin>>num; 55 scanf("%d %d %d",&cg,&mg,&eg); 56 ap[i].id=ep[i].id=cp[i].id=mp[i].id=num; 57 ap[i].grade=int((cg+mg+eg)*1.0/3+0.5); //四舍五入 58 ep[i].grade=eg; 59 mp[i].grade=mg; 60 cp[i].grade=cg; 61 } 62 63 64 sort(ap,ap+n,cmpa); 65 int cal=1; 66 somenode p; 67 p.r="A"; 68 p.rank=cal; 69 some[ap[0].id]=p; 70 for(i=1;i<n;i++){ 71 if(ap[i].grade!=ap[i-1].grade){ 72 cal=i+1; 73 } 74 p.r="A"; 75 p.rank=cal; 76 some[ap[i].id]=p; 77 } 78 79 80 sort(cp,cp+n,cmpc); 81 cal=1; 82 if(some[cp[0].id].rank>cal){ 83 some[cp[0].id].rank=cal; 84 some[cp[0].id].r="C"; 85 } 86 for(i=1;i<n;i++){ 87 if(cp[i].grade!=cp[i-1].grade){ 88 cal=i+1; 89 } 90 if(some[cp[i].id].rank>cal){ 91 some[cp[i].id].rank=cal; 92 some[cp[i].id].r="C"; 93 } 94 } 95 96 97 sort(mp,mp+n,cmpm); 98 cal=1; 99 if(some[mp[0].id].rank>cal){ 100 some[mp[0].id].rank=cal; 101 some[mp[0].id].r="M"; 102 } 103 for(i=1;i<n;i++){ 104 if(mp[i].grade!=mp[i-1].grade){ 105 cal=i+1; 106 } 107 if(some[mp[i].id].rank>cal){ 108 some[mp[i].id].rank=cal; 109 some[mp[i].id].r="M"; 110 } 111 } 112 113 114 sort(ep,ep+n,cmpe); 115 cal=1; 116 if(some[ep[0].id].rank>cal){ 117 some[ep[0].id].rank=cal; 118 some[ep[0].id].r="E"; 119 } 120 for(i=1;i<n;i++){ 121 if(ep[i].grade!=ep[i-1].grade){ 122 cal=i+1; 123 } 124 if(some[ep[i].id].rank>cal){ 125 some[ep[i].id].rank=cal; 126 some[ep[i].id].r="E"; 127 } 128 } 129 130 131 map<string,somenode>::iterator it; 132 for(i=0;i<m;i++){ 133 cin>>num; 134 it=some.find(num); 135 if(it==some.end()){ 136 cout<<"N/A"<<endl; 137 } 138 else{ 139 cout<<some[num].rank<<" "<<some[num].r<<endl; 140 } 141 } 142 143 delete []ap; 144 delete []cp; 145 delete []mp; 146 delete []ep; 147 return 0; 148 }