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 (≤105), 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 ScoreB/1.5 + ScoreA + ScoreT*1.5
, where ScoreX
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
数组写在main函数外防止爆栈,还是没通过超时,可能两层for循环?
#include <iostream>
#include<cstdlib>
#include<cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
struct testees
{
char id[10];
double s;
int score;
char school[50];
int ns;
int rank;
};
void change(char str[])
{
for(int i=0; i<strlen(str); i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=('a'-'A');
}
}
}
int cmp(testees a,testees b)
{
if(a.score!=b.score)
return a.score>b.score;
else if(a.ns!=b.ns)
return a.ns<b.ns;
else return strcmp(a.school,b.school)<0;
}
int cmp2(testees a,testees b)
{
return strcmp(a.school,b.school)>0;
}
testees test[101000],test2[101000];
int main()
{
int n;
scanf("%d",&n);
char id[10],school[10];
for(int i=0; i<n; i++)
{
scanf("%s%d%s",id,&test[i].score,school);
strcpy(test[i].id,id);
change(school);
strcpy(test[i].school,school);
//printf("%s\n",test[i].school);
if(test[i].id[0]=='A')
{
test[i].s+=test[i].score;
}
else if(test[i].id[0]=='B')
{
test[i].s+=test[i].score/1.5;
}
else if(test[i].id[0]=='T')
{
test[i].s+=test[i].score*1.5;
}
}
sort(test,test+n,cmp2);
strcpy(test2[0].school,test[0].school);
test2[0].s=test[0].s;
test2[0].ns++;
int count=0;
for(int i=1; i<n; i++)
{
if(strcmp(test2[count].school,test[i].school)==0)
{
test2[count].s=test2[count].s+test[i].s;
test2[count].ns++;
}
else
{
count++;
strcpy(test2[count].school,test[i].school);
test2[count].s=test[i].s;
test2[count].ns++;
}
}
for(int i=0; i<=count; i++)
{
test2[i].score=(int)test2[i].s;
}
sort(test2,test2+count+1,cmp);
printf("%d\n",count+1);
test2[0].rank=1;
printf("%d %s %d %d\n",test2[0].rank,test2[0].school,test2[0].score,test2[0].ns);
for(int i=1; i<=count; i++)
{
if(test2[i].score==test2[i-1].score)
{
test2[i].rank=test2[i-1].rank;
}
else
{
test2[i].rank=i+1;
}
printf("%d %s %d %d\n",test2[i].rank,test2[i].school,test2[i].score,test2[i].ns);
}
return 0;
}
只用了一个结构体,不再两层for循环嵌套
用strcmp,需要头文件<string.c>,且是char数组类型相比较。
两个数组test[],test2[]. test[]存储初始信息,test2[]存储经过比较之后的结果信息。
#include <iostream>
#include<cstdlib>
#include<cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
struct testees
{
char id[10];
double s;
int score;
char school[50];
int ns;
int rank;
};
void change(char str[])
{
for(int i=0; i<strlen(str); i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=('a'-'A');
}
}
}
int cmp(testees a,testees b)
{
if(a.score!=b.score)
return a.score>b.score;
else if(a.ns!=b.ns)
return a.ns<b.ns;
else return strcmp(a.school,b.school)<0;
}
int cmp2(testees a,testees b)
{
return strcmp(a.school,b.school)>0;
}
testees test[101000],test2[101000];
int main()
{
int n;
scanf("%d",&n);
char id[10],school[10];
for(int i=0; i<n; i++)
{
scanf("%s%d%s",id,&test[i].score,school);
strcpy(test[i].id,id);
change(school);
strcpy(test[i].school,school);
if(test[i].id[0]=='A')
{
test[i].s+=test[i].score;
}
else if(test[i].id[0]=='B')
{
test[i].s+=test[i].score/1.5;
}
else if(test[i].id[0]=='T')
{
test[i].s+=test[i].score*1.5;
}
}
sort(test,test+n,cmp2);
strcpy(test2[0].school,test[0].school);
test2[0].s=test[0].s;
test2[0].ns++;
int count=0;
for(int i=1; i<n; i++)
{
if(strcmp(test2[count].school,test[i].school)==0)
{
test2[count].s=test2[count].s+test[i].s;
test2[count].ns++;
}
else
{
count++;
strcpy(test2[count].school,test[i].school);
test2[count].s=test[i].s;
test2[count].ns++;
}
}
for(int i=0; i<=count; i++)
{
test2[i].score=(int)test2[i].s;
}
sort(test2,test2+count+1,cmp);
printf("%d\n",count+1);
test2[0].rank=1;
printf("%d %s %d %d\n",test2[0].rank,test2[0].school,test2[0].score,test2[0].ns);
for(int i=1; i<=count; i++)
{
if(test2[i].score==test2[i-1].score)
{
test2[i].rank=test2[i-1].rank;
}
else
{
test2[i].rank=i+1;
}
printf("%d %s %d %d\n",test2[i].rank,test2[i].school,test2[i].score,test2[i].ns);
}
return 0;
}