原题地址:PAT甲级1047
题目正文:
1047 Student List for Course(25 分)
Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤40,000), the total number of students, and K (≤2,500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (≤20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.
Output Specification:
For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.
Sample Input:
10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5
Sample Output:
1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
题目要求:
作者: CHEN, Yue
单位: 浙江大学
时间限制: 1000ms
内存限制: 64MB
代码长度限制: 16KB
说明:
这个题包含大量的输入输出、排序操作,如果不进行一定的优化,那么很容易超时。陈越姥姥曾说过,题目时间上限是C标程的时间放大3~5倍,再加100毫秒。然而标程不公开的。按照姥姥给出的题目时间设定准则,我们的目标就是达到类似的时间效率。
我们通过读题,观察到学生的姓名设定比较统一,那么每次的姓名输出使用putchar()循环四次输出一个学生的姓名会比printf("%s\n",char *)这种字符串输出快很多。同理,使用getchar()循环读入学生的姓名也会较scanf()快很多。实际上,对于整数的读入,在这里我们手动实现一个基于getchar()的正整数的读取操作也会加速我们的程序运行。
在对于每个课程进行人员统计时,我们无须保存姓名的字符串完整表示,只需要保存姓名该学生的姓名在姓名存储矩阵中的index即可,然后根据字母顺序进行排序,这里我们使用辅助数组对姓名顺序进行预排序免除频繁的字符串比较操作。
通过实现以上的这些想法,我们给出最终的运行时间截图和完整版程序。
运行时间截图:
完整AC代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
char names[40010][5];//student names, index from 1 to n;
int r[40010]; //r[node-id]=rank order, where node-id is the name-index in names[]
int mat[40010]; // temporary matrix that stores node-ids, where node-id was aforementioned
int cmp1(int a,int b){ // sort ids according to alphabetical order
return strcmp(names[a],names[b])<0;
}
int cmp2(int a,int b){ // sort ids according to rank
return r[a]<r[b];
}
int read(){ //fast version of scanf("%d",&number));
int input = 0;
char a = getchar();
while(a<'0' || a>'9')
a = getchar();
while(a>='0' && a<='9'){
input=input*10+a-'0';
a=getchar();
}
return input;
}
int main(){
int n,k,tmpn,tmp,index;
scanf("%d %d",&n,&k);
getchar(); // throw '\n'
vector<int> c[k+1]; // courses, index from 1 to k
for(int i=1;i<=n;i++){
for(int j=0;j<4;j++) //try to use getchar() to speed names reading
names[i][j]=getchar();
tmpn=read();
for(int j=0;j<tmpn;j++){ //vector.push_back() reading
tmp=read();
c[tmp].push_back(i);
}
mat[i-1]=i;
}
// sort names index according to alphabetical order
sort(mat,mat+n,cmp1);
for(int i=0;i<n;i++)
r[mat[i]]=i; // r[node-id]=node-rank, where node-id is the name-index in names[],\
//and node-rank is in the range of [0,n)
for(int i=1;i<=k;i++){
printf("%d %d\n",i,c[i].size());\
sort(c[i].begin(),c[i].end(),cmp2);
for(int j=0;j<c[i].size();j++){
for(int k=0;k<4;k++)
putchar(names[c[i][j]][k]);
putchar('\n');
}
}
return 0;
}
C标程的参考时间为180 ms - 300 ms之间,程序基本达到了这个时间要求。
略陈拙见,多有不周,望多指教。