1047. Student List for Course (25)

﹏ヽ暗。殇╰゛Y 2022-05-31 12:56 221阅读 0赞

Zhejiang University has 40000 students and provides 2500 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 (<=40000), the total number of students, and K (<=2500), 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:

  1. 10 5
  2. ZOE1 2 4 5
  3. ANN0 3 5 2 1
  4. BOB5 5 3 4 2 1 5
  5. JOE4 1 2
  6. JAY9 4 1 2 5 4
  7. FRA8 3 4 2 5
  8. DON2 2 4 5
  9. AMY7 1 5
  10. KAT3 3 5 4 2
  11. LOR6 4 2 4 1 5

Sample Output:

  1. 1 4
  2. ANN0
  3. BOB5
  4. JAY9
  5. LOR6
  6. 2 7
  7. ANN0
  8. BOB5
  9. FRA8
  10. JAY9
  11. JOE4
  12. KAT3
  13. LOR6
  14. 3 1
  15. BOB5
  16. 4 7
  17. BOB5
  18. DON2
  19. FRA8
  20. JAY9
  21. KAT3
  22. LOR6
  23. ZOE1
  24. 5 9
  25. AMY7
  26. ANN0
  27. BOB5
  28. DON2
  29. FRA8
  30. JAY9
  31. KAT3
  32. LOR6
  33. ZOE1

题目大意:

浙江大学有40000名学生,提供2500门课程。现在根据每个学生的课程注册列表,你应该输出所有课程的学生名单。
输入规格:
每个输入文件包含一个测试用例。对于每个测试用例,第一行包含两个数字:N(<=40000),是学生的总数,K(<=2500),是课程总数。然后是N行,每行包含一个学生的名字(3个大写英文字母加上一个1位数字),一个正整数C(<=20),这个是学生注册的课程的数量,然后是C个课程的编号。为了简单起见,课程编号从1到K。
输出规范:
对于每个测试用例,一课程编号的递增顺序打印所有课程的的学生姓名列表。对于每一门课程,首先在一行上打印出课程编号和和注册学生的数量,用空格隔开。然后按字母顺序输出学生的名字。每个名字占一行。

代码:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<string.h>
  5. using namespace std;
  6. struct node
  7. {
  8. char name[5];
  9. }stud[180000];
  10. vector<int> course[2501];
  11. bool cmp(int a,int b)
  12. {
  13. return a<b;
  14. }
  15. int change(char a[])
  16. {
  17. return (a[0]-'A')*26*26*10+(a[1]-'A')*26*10+(a[2]-'A')*10+a[3]-'0';
  18. }
  19. int main()
  20. {
  21. int i,j,n,m,k,t,id;
  22. char name[5];
  23. scanf("%d %d",&n,&m);
  24. for(i=0;i<n;i++)
  25. {
  26. scanf("%s %d",name,&k);
  27. id=change(name);
  28. strcpy(stud[id].name,name);
  29. for(j=0;j<k;j++)
  30. {
  31. scanf("%d",&t);
  32. course[t].push_back(id);
  33. }
  34. }
  35. for(i=1;i<=m;i++)
  36. {
  37. printf("%d %d\n",i,course[i].size());
  38. sort(course[i].begin(),course[i].end(),cmp);
  39. for(j=0;j<course[i].size();j++)
  40. {
  41. printf("%s\n",stud[course[i][j]].name);
  42. }
  43. }
  44. return 0;
  45. }

超时代码:不超时的话下面的代码更清晰明了。

70

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<algorithm>
  5. #include<map>
  6. #include<vector>
  7. using namespace std;
  8. map<int,vector<string> > Map;
  9. bool cmp(string a,string b)
  10. {
  11. return a<b;
  12. }
  13. int main()
  14. {
  15. int i,j,n,m,k,t;
  16. string s;
  17. cin>>n>>m;
  18. for(i=0;i<n;i++)
  19. {
  20. cin>>s>>k;
  21. for(j=0;j<k;j++)
  22. {
  23. cin>>t;
  24. Map[t].push_back(s);
  25. }
  26. }
  27. for(i=1;i<=m;i++)
  28. {
  29. sort(Map[i].begin(),Map[i].end(),cmp);
  30. cout<<i<<" "<<Map[i].size()<<endl;
  31. for(j=0;j<Map[i].size();j++)
  32. {
  33. cout<<Map[i][j]<<endl;
  34. }
  35. }
  36. return 0;
  37. }

发表评论

表情:
评论列表 (有 0 条评论,221人围观)

还没有评论,来说两句吧...

相关阅读