-
题目大意:
-
思路1:因为set会自动排序,所以用结构体数组存学生的课表,用结构体数组存每节课的学生(不用排序,因为是按照顺序输出课程)。
超时了(用太多stl容器)
-
知识点:
- 结构体
- set
- vector
-
代码:
#include <iostream> #include <set> #include <vector> using namespace std; struct stu{ string name; set<int> crs; }; struct course{ int id; set<string> registered_stu; // set会自动排序 }; int main(){ int n, k, c, cid; string name; scanf("%d %d", &n, &k); vector<stu> students(n); vector<course> courses(k); for(int i = 0; i < n; i++){ cin >> name >> c; students[i].name = name; for(int j = 0; j < c; j++){ cin >> cid; students[i].crs.insert(cid); } } for(int i = 0; i < k; i++){ for(int j = 0; j < n; j++){ if(students[j].crs.find(i + 1) != students[j].crs.end()){ // 这个学生选了该课 courses[i].id = i + 1; courses[i].registered_stu.insert(students[j].name); } } } for(int i = 0; i < k; i++){ printf("%d %d\n", i + 1, courses[i].registered_stu.size()); // 输出courses[i].id时答案错误,因为如果没有学生选这个课,就没有course[i].id for(auto j = courses[i].registered_stu.begin(); j != courses[i].registered_stu.end(); j++){ printf("%s\n", (*j).c_str()); } } return 0; }
-
-
思路2:用二维数组
course[i][j]
存选每节课的学生,course[i]
表示第i+1节课,course[i][j]
表示选该课的学生id。 然后再用sort()
给每个course[i]
排序。如果用集合数组(
set<string> student[2510]
)还是超时,因为用了set-
知识点:
- 二维数组
- vector
- 排序
-
代码:
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<string> courses[2510]; // 二维数组,因为每节课学生的数量不确定,所以用set,方便插入元素 bool cmp(string stu1, string stu2){ return stu1 < stu2; } int main(){ int n, k, c, cid, i, j; scanf("%d %d", &n, &k); for(i = 0; i < n; i++){ string student; cin >> student >> c; for(j = 0; j < c; j++){ scanf("%d", &cid); courses[cid].push_back(student); } } for(i = 1; i <= k; i++){ printf("%d %d\n", i, courses[i].size()); sort(courses[i].begin(), courses[i].end(), cmp); for(auto it = courses[i].begin(); it != courses[i].end(); it++) printf("%s\n", (*it).c_str()); } return 0; }
-
-
总结:
-
又理解错了题意。"
print the student name lists of all the courses in increasing order of the course numbers
" 应该是按照字典序升序输出各个课程的学生,我却理解成了按照学生数升序输出各个课程的学生。以后应该更仔细一点!
-
set太耗时了。我已经做到好几道题因为用了set而超时了。
set
内部由红黑树(一种平衡二叉查找树)实现,unordered_set
由散列实现,所以**unordered_set
速度比set
快得多**。map
内部也由红黑树(一种平衡二叉查找树)实现,unordered_map
由散列实现,所以**unordered_map
速度比set
快得多**。
-
二维数组:
-
int a[10][10]
-
vector<int> a[10]
-
char a[10][10]
:字符串数组
-
-
【PAT】1047 Student List for Course (25 分)
最新推荐文章于 2022-04-06 16:39:29 发布