题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805433955368960

这题是上一题的反转

  1. 像这种数据范围很大的题目,最好都使用char,使用string容易超时
  2. 这题最值得借鉴的是二维数组存储姓名,但也容易搞混
  3. 另一个值得借鉴的地方是,一般结果按顺序输出啥,就建立那个对象的vector数组,比如这道题要输出每门课的选课名单,那么最后一轮for循环一定是课程,因此建立课程的vector二维数组

代码

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<vector>
  4. #include<cstring>
  5. using namespace std;
  6. const int maxn_of_stu = 40010;
  7. const int maxn_of_course = 2510;
  8. char name[maxn_of_stu][5];
  9. bool cmp(int a, int b){
  10. return strcmp(name[a], name[b]) < 0;//按字典序从小到大排序
  11. }
  12. int main(){
  13. int stu_num, course_num, temp;
  14. vector<int> course[maxn_of_course];
  15. int course_temp;
  16. scanf("%d%d",&stu_num, &course_num);
  17. for(int i = 0; i < stu_num; i++){
  18. scanf("%s %d",name[i], &course_temp);
  19. for(int j = 0; j < course_temp; j++){
  20. scanf("%d",&temp);
  21. course[temp].push_back(i);
  22. }
  23. }
  24. for(int i = 1; i <= course_num; i++){
  25. int len = course[i].size();
  26. sort(course[i].begin(),course[i].end(), cmp);
  27. printf("%d %d\n", i, len);
  28. for(int j = 0; j < len; j++){
  29. printf("%s\n", name[course[i][j]]);
  30. }
  31. }
  32. }