数据结构实验之排序七:选课名单
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
随着学校规模的扩大,学生人数急剧增加,选课名单的输出也成为一个繁重的任务,我校目前有在校生3万多名,两千多门课程,请根据给定的学生选课清单输出每门课的选课学生名单。
Input
输入第一行给出两个正整数N( N ≤ 35000)和M(M ≤ 2000),其中N是全校学生总数,M是课程总数,随后给出N行,每行包括学生姓名拼音+学号后两位(字符串总长度小于10)、数字S代表该学生选课的总数,随后是S个课程编号,约定课程编号从1到M,数据之间以空格分隔。
Output
按课程编号递增的顺序输出课程编号、选课总人数以及选课学生名单,对选修同一门课程的学生按姓名的字典序输出学生名单。数据之间以空格分隔,行末不得有多余空格。
Example Input
5 3
Jack01 2 2 3
Jone01 2 1 3
Anni02 1 1
Harry01 2 1 3
TBH27 1 1
Example Output
1 4
Anni02
Harry01
Jone01
TBH27
2 1
Jack01
3 3
Harry01
Jack01
Jone01
Hint
Author
xam
Think:
这道题目,我刚开始的思路是用人作为结构体,写来写去还是超时……
TLE:
#include <bits/stdc++.h>
using namespace std;
struct node
{
char s[20];
int a[4456];
}t[65560];
int sum[4456];
int cmp(const void *c, const void * d)
{
struct node *a = (struct node *)c;
struct node *b = (struct node *)d;
return strcmp(a->s, b->s);
}
int main()
{
int n, m, x, y;
cin>>n>>m;
memset(sum, 0, sizeof(sum));
for(int i=0;i<n;i++)
{
scanf("%s", t[i].s);
memset(t[i].a, 0, sizeof(t[i].a));
cin>>x;
for(int j=0;j<x;j++)
{
cin>>y;
t[i].a[y] = 1;
sum[y]++;
}
}
qsort(t, n, sizeof(t[0]), cmp);
for(int i=1;i<=m;i++)
{
cout<<i<<" "<<sum[i]<<endl;
for(int j=0;j<n;j++)
{
if(t[j].a[i])
cout<<t[j].s<<endl;
}
}
return 0;
}
/***************************************************
User name:
Result: Time Limit Exceeded
Take time: 1010ms
Take Memory: 0KB
Submit time:
****************************************************/
然后,我开始考虑可不可以用课程作为结构体,这就当成是一种新的思路吧,以后不知道会不会用得到。
AC:
#include <bits/stdc++.h>
using namespace std;
struct node
{
string p[12313];
int count;
}t[2111];
int main()
{
int n, m;
cin>>n>>m;
int x, y;
string ch;
for(int i=1;i<=m;i++)
t[i].count = 0;
while(n--)
{
cin>>ch;
cin>>x;
for(int i=1;i<=x;i++)
{
cin>>y;
t[y].p[t[y].count++] = ch;
}
}
for(int i=1;i<=m;i++)
{
cout<<i<<" "<<t[i].count<<endl;
sort(t[i].p, t[i].p+t[i].count);
for(int j=0;j<t[i].count;j++)
{
cout<<t[i].p[j]<<endl;
}
}
return 0;
}
/***************************************************
User name:
Result: Accepted
Take time: 316ms
Take Memory: 7668KB
Submit time:
****************************************************/