1141 PAT Ranking of Institutions (25)

Posted mr-stn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1141 PAT Ranking of Institutions (25)相关的知识,希望对你有一定的参考价值。

After each PAT, the PAT Center will announce the ranking of institutions based on their students‘ performances. Now you are asked to generate the ranklist.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10^5^), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

ID Score School

where "ID" is a string of 6 characters with the first one representing the test level: "B" stands for the basic level, "A" the advanced level and "T" the top level; "Score" is an integer in [0, 100]; and "School" is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that "ID" is unique for each testee.

Output Specification:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

Rank School TWS Ns

where "Rank" is the rank (start from 1) of the institution; "School" is the institution code (all in lower case); "TWS" is the total weighted score which is defined to be the integer part of "Score~B~/1.5 + Score~A~ + Score~T~*1.5", where "Score~X~" is the total score of the testees belong to this institution on level X; and "Ns" is the total number of testees who belong to this institution.

The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

Sample Input:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

pat里面恶心人的大概就是排序了吧, pat的点真是奇怪呢!
题目大意:给出考生的姓名, 分数,学校,对每个学校按照总分排序, 分数相同的按照参加人数排序, 还是相同,则按照学校名字排序,学校名字同意转换为小写;
思路:用vector<node> v保存学校的参考人数, 总分; 用map<string, int>来保存每一个学校在v中的储存位置; 设计好比较函数,排序确定排名输出;
注意点:排序,加上字符串就比较坑了;
  • string的输入输出为耗费大量时间,但是string的输入又不能用高效的scanf()所以string用cin输入输出,学生姓名,以及成绩都用scanf(),printf()输入输出。这样才能避免超时;
  • 总分的计算是最后才取整数,不能每次都取整数,否则最后一个测试点不能通过; 因为我node中total用的是double保存,所以在比较函数,和输出的时候,只要存在两个对象的总分要进行比较,都要先转换为整数,在进行比较,否则最后一个测试点还是错的
  • 本来尝试用char*作为map的关键词,这样可以避免string的输入,奈何不会。 pat有的时候,考点真的是奇怪,摸不着头脑
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<string>
 5 #include<map>
 6 using namespace std;
 7 class node{
 8 public:
 9   string id;
10   int ns;
11   double total;
12   node(){total=0.0; ns=0;}
13 };
14 
15 void convert(string &s){
16   for(int i=0; i<s.size(); i++)
17   if(s[i]<=Z&&s[i]>=A) s[i] += 32;
18 }
19 bool cmp(node a, node b){
20   int x=(int)a.total, y=(int)b.total;
21   if(x!=y) return x>y;
22   if(a.ns!=b.ns) return a.ns<b.ns;
23   return a.id<b.id;
24 }
25 
26 int main(){
27   int n, i;
28   cin>>n;
29   map<string, int> mmap;
30   vector<node> v(n);
31   int cnt=0;
32   for(i=0; i<n; i++){
33     string  sch;
34     char name[8];
35     int index;
36     int s;
37     scanf("%s %d", name, &s); cin>>sch;
38     convert(sch);
39     double score=s*1.0;
40     if(name[0]==A) score *= 1.0;
41     else if(name[0]==B) score/=1.5;
42     else score *= 1.5;
44     if(mmap.count(sch)==0) mmap[sch]=cnt++;
45     index=mmap[sch];
46     v[index].ns++;  v[index].id=sch; v[index].total += score;
47   }
48   sort(v.begin(), v.begin()+cnt, cmp);
49   cout<<cnt<<endl;
50   int rank=1, last=(int)v[0].total;
51   for(i=0; i<cnt; i++){
52     int now=(int)v[i].total;
53     if(i==0) printf("1 ");
54     else{
55       if(now==last) printf("%d ", rank);
56       else{printf("%d ", i+1); rank=i+1; last=(int)v[i].total;}
57     }
58     cout<<v[i].id;
59     printf(" %d %d
", now, v[i].ns);
60   }
61   return 0;
62 }

 






以上是关于1141 PAT Ranking of Institutions (25)的主要内容,如果未能解决你的问题,请参考以下文章

1141 PAT Ranking of Institutions (25)

PAT甲级1141PAT Ranking of Institutions

A 1141 PAT Ranking of Institutions (25分)

1141 PAT Ranking of Institutions (25 分)难度: 一般 / 知识点: 排序

PAT甲级——A1025 PAT Ranking

PAT A1025 PAT Ranking(25)