1012 The Best Rank (25分)

 

 易错点:当有并列的时候(比如并列第一),正确排序应该是1,1,3,4,5而不是1,1,2,3,4。

我突然想到,我很不喜欢开大数组,喜欢节省空间,有时候这样反而不好,因为oj往往空间限制要求很低,pat基本上给了64MB,这应该是足够的,往往时间会有限制,一旦数据很多很容易超时。用空间换时间是一个小技巧吧。对比了大神的代码,别人用的空间少,用时也少,代码也更简洁,跪了。不过也许还有另一个原因,scanf和printf在用时上比cin和cout要短,据说会省时一半。

我用map的原因是,没有注意到上面的易错点,本想用遍历查找id来确定排序,而map按照id来查找十分方便,但按id确定排序在处理并列时非常麻烦,所以直接按分数确定排序即可,这样看来map好像也没有必要了。

我的:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include <algorithm>
 5 #include<map> 
 6 using namespace std;
 7 bool com(pair<string,int> a,pair<string,int> b){
 8     return a.second>b.second;
 9 }
10 int findRank(vector<pair<string,int> > x,int score){
11     int size=x.size();
12     for(int i=0;i<size;i++)
13     {
14         if(score==x[i].second)
15            return i+1;
16         
17     }
18 }
19 int main(){  
20     string id,id2;
21     int sC,sM,sE;
22     map<string,int> A,C,M,E;
23     int numN,numM;
24     cin>>numN>>numM;
25     vector<pair<string,int> > SORTA,SORTC,SORTM,SORTE;
26     for(int i=0;i<numN;i++)
27     {
28         cin>>id>>sC>>sM>>sE;
29         A[id]=(sC+sM+sE)/3;C[id]=sC;
30         M[id]=sM;E[id]=sE;
31         SORTA.push_back(make_pair(id,(sC+sM+sE)/3));SORTC.push_back(make_pair(id,sC));
32         SORTM.push_back(make_pair(id,sM));SORTE.push_back(make_pair(id,sE));
33     }
34     sort(SORTA.begin(),SORTA.end(),com);sort(SORTC.begin(),SORTC.end(),com);
35     sort(SORTM.begin(),SORTM.end(),com);sort(SORTE.begin(),SORTE.end(),com);
36     map<string,int>::iterator it;
37     int highest,flag=1,temp;
38     vector<string> MMM;
39     for(int i=0;i<numM;i++)
40     {    cin>>id2;MMM.push_back(id2);    
41     }
42     for(int i=0;i<numM;i++)
43     {   id2=MMM[i];
44         if(A.find(id2)==A.end())
45             cout<<"N/A";
46         else
47         {
48         highest=findRank(SORTA,A[id2]);
49         flag=1;
50         temp=findRank(SORTC,C[id2]);
51         if(temp<highest){
52             highest=temp;flag=2;
53         }
54         temp=findRank(SORTM,M[id2]);
55         if(temp<highest){
56             highest=temp;flag=3;
57         }
58         temp=findRank(SORTE,E[id2]);
59         if(temp<highest){
60             highest=temp;flag=4;
61         }
62         if(flag==1)
63         {cout<<highest<<" "<<'A';
64         }
65         else if(flag==2)
66         {cout<<highest<<" "<<'C';
67         }
68         else if(flag==3)
69         {cout<<highest<<" "<<'M';
70         }
71         else
72         {cout<<highest<<" "<<'E';
73         }
74         }
75         if(i!=numM-1)
76            cout<<endl;
77      } 
78 return 0;
79 }

测评情况:

 

 大神的:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 struct node {
 5     int id, best;
 6     int score[4], rank[4];
 7 }stu[2005];
 8 int exist[1000000], flag = -1;
 9 bool cmp1(node a, node b) {return a.score[flag] > b.score[flag];}
10 int main() {
11     int n, m, id;
12     scanf("%d %d", &n, &m);
13     for(int i = 0; i < n; i++) {
14         scanf("%d %d %d %d", &stu[i].id, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
15         stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;
16     }
17     for(flag = 0; flag <= 3; flag++) {
18         sort(stu, stu + n, cmp1);
19         stu[0].rank[flag] = 1;
20         for(int i = 1; i < n; i++) {
21             stu[i].rank[flag] = i + 1;
22             if(stu[i].score[flag] == stu[i-1].score[flag])
23                 stu[i].rank[flag] = stu[i-1].rank[flag];
24         }
25     }
26     for(int i = 0; i < n; i++) {
27         exist[stu[i].id] = i + 1;
28         stu[i].best = 0;
29         int minn = stu[i].rank[0];
30         for(int j = 1; j <= 3; j++) {
31             if(stu[i].rank[j] < minn) {
32                 minn = stu[i].rank[j];
33                 stu[i].best = j;
34             }
35         }
36     }
37     char c[5] = {'A', 'C', 'M', 'E'};
38     for(int i = 0; i < m; i++) {
39         scanf("%d", &id);
40         int temp = exist[id];
41         if(temp) {
42             int best = stu[temp-1].best;
43             printf("%d %c\n", stu[temp-1].rank[best], c[best]);
44         } else {
45             printf("N/A\n");
46         }
47     }
48     return 0;
49 }
View Code

对应测评:

大神的博客地址: https://www.liuchuo.net/archives/2207

posted @ 2020-03-11 22:35  wsshub  阅读(210)  评论(0)    收藏  举报