A1047
Description:
浙江大学有很多课程和学生。现在给出每个学生的已选课程,求所有课程的学生名单。
Input:
- 每个测试一组样例;
- 首行给出两个数字:学生数N<=40 000,总课程数K<= 2500;
- 接下来N行,每行包含:一个学生的姓名(3位大写英文字母+1位数字)、学生已选课程数C<=20,接下来C个数字表示课程的编号,课程号从1-K
Output:
- 每个测试,根据课程编号升序输出所有课程的学生名单;
- 对于每门课,首行输出课程编号以及选课学生数;
- 接下来输出学生名字,按照字母表顺序
总结:
- 审题仔细——“所有课程”
- 算法耗时占比较小,时间都浪费在输入输出,所以一定要注意输入输出使用printf,若是string类型记得使用string.c_str()。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int n, k;
char stu[40005][10];
vector<int>course[2505];
bool cmp(int a, int b){
return strcmp(stu[a], stu[b]) < 0;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, k;
char name[10];
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++){
int c, cid;
scanf("%s%d", stu[i], &c);
for(int j = 0; j < c; j++){
scanf("%d", &cid);
course[cid].push_back(i);
}
}
for(int i = 1; i <= k; i++){
printf("%d %d\n", i, course[i].size());
sort(course[i].begin(), course[i].end(), cmp);
for(auto it : course[i])
printf("%s\n", stu[it]);
}
return 0;
}