PAT1047 Student List for Course (25分)避免运行超时(内存溢出)

2020-07-14 10:54:42 浏览数 (1)

题目

Zhejiang University has 40,000 students and provides 2,500 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 (≤40,000), the total number of students, and K (≤2,500), 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:

代码语言:javascript复制
10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

Sample Output:

代码语言:javascript复制
1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

解析

题意:

给出n名学生所选择的课程,输出k门课程每门课程的编号,选课人数,选课学生姓名列表。

思路:

  • 给出学生选课列表,输出课程的学生列表,只需要在输入的时候做一个转换:比如 A选择了123课程,则课程1选课人列表中添加A课程2选课列表中添加A课程3选课列表中添加A即可。
  • 之后,对每个课程的选课列表按照学生姓名进行排序即可。

注意:

  • 题目给出每个学生的姓名是三个大写英文字母加1个数字,所以,请使用char[40000][5]保存学生姓名,使用string[40000]会内存溢出,最后一个测试点运行超时。
  • 尽量使用scanfprintfcincout更加耗时,也可能造成运行超时。
  • 如果使用vector存储每个课程的学生列表,请以数组的方式进行遍历,比如
代码语言:javascript复制
	vector<int> course[2501];
	for(int j = 0; j < course[i].size(); j  )
            printf("%sn", name[course[i][j]]);
    // 不要使用foreach,如 
    for(int j: course[i]) cout << name[i][j];  
    // 否则也可能会内存溢出

代码

代码语言:javascript复制
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
// 40000个学生名字,每个名字是三个大写字母加一个数字
char name[40000][5];
// string name[40000]; // 会内存溢出
vector<int> course[2501];
// 每个课程的所有学生按姓名排序
bool cmp(int a, int b) {
    return name[a] < name[b];
//     return strcmp(name[a], name[b]) < 0;
}
int main() {
    // n个学生,k门课
    int n, k;
    cin >> n >> k;
    int cnum, cid;
    for (int i = 0; i < n;   i) {
        // 学生姓名 选了几门课
        cin >> name[i] >> cnum;
        // 每门课的编号
        while (cnum-- > 0) {
            cin >> cid;
            // 这门课选课人包含这个学生
            course[cid].push_back(i);
        }
    }
    // 一共k门课
    for (int i = 1; i <= k;   i) {
        // 课程编号和选课人数
        cout << i << " " << course[i].size() << endl;
        // 全部选课学生按姓名排序
        sort(course[i].begin(), course[i].end(), cmp);
        // 输出每个选课学生的姓名
        for(int j = 0; j < course[i].size(); j  )
            printf("%sn", name[course[i][j]]);
            //  cout << name[course[i][j]] << endl; // 会超时
    }

    return 0;
}

0 人点赞