一开始是建立了course[2501][40001]数组,存储每节课的学生编号
然后for循环两层输出,但这样复杂度为O(2500*40000),也很明显导致最后时间超时
后来发现最多40000学生,每个学生最多选20门课,那么总共也就40000*20
所以直接就存储学生-课程的信息,然后排个序,按照课程从小到大,课程一样的话则按字典序
然后从头扫一遍即可,复杂度O(80000)
不过要注意一点,有些课可能并没有出现,所以要做个判断,输出x 0。

#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <cstdio>
using namespace std; const int maxn=+;
char id_name[maxn][];
struct Stu{
char name[];
int course;
bool operator<(const Stu tmp)const{
if(course==tmp.course){
if(strcmp(name,tmp.name)<=)
return true;
else
return false;
}
else
return course<tmp.course;
}
}stu[maxn*];
int main()
{
int n,k;
scanf("%d %d",&n,&k);
int idx=;
int c;
char name[];
int num[];
memset(num,,sizeof(num));
for(int i=;i<n;i++){
//cin>>stu[i].name;
scanf("%s",name);
scanf("%d",&c);
for(int j=;j<c;j++){
scanf("%d",&stu[idx].course);
strcpy(stu[idx].name,name);
num[stu[idx].course]++;
idx++;
}
//sort(stu[i].course,stu[i].course+stu[i].c);
}
sort(stu,stu+idx);
int last=,now=;
for(int i=;i<idx;i++){
if(i==){
now=stu[i].course;
for(int j=;j<now;j++)
printf("%d 0\n",j); //没有出现的课,也要输出0
printf("%d %d\n",stu[i].course,num[stu[i].course]);
printf("%s\n",stu[i].name);
last=stu[i].course;
}
else{
if(stu[i].course!=stu[i-].course){
now=stu[i].course;
for(int j=last+;j<now;j++)
printf("%d 0\n",j); //没有出现的课,也要输出0
printf("%d %d\n",stu[i].course,num[stu[i].course]);
printf("%s\n",stu[i].name);
last=stu[i].course;
}
else{
printf("%s\n",stu[i].name);
}
}
}
return ;
}

PAT甲级题解-1047. Student List for Course (25)-排序的更多相关文章

  1. 【PAT甲级】1047 Student List for Course (25 分)

    题意: 输入两个正整数N和K(N<=40000,K<=2500),接下来输入N行,每行包括一个学生的名字和所选课程的门数,接着输入每门所选课程的序号.输出每门课程有多少学生选择并按字典序输 ...

  2. PAT 解题报告 1047. Student List for Course (25)

    1047. Student List for Course (25) Zhejiang University has 40000 students and provides 2500 courses. ...

  3. PAT (Advanced Level) 1047. Student List for Course (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  4. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  5. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)

    1047 Student List for Course (25 分)   Zhejiang University has 40,000 students and provides 2,500 cou ...

  7. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  8. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  9. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

随机推荐

  1. linux操作系统基础讲解

    计算机的组成及功能: 现在市场上的计算机组成结构遵循冯 诺依曼体系,由CPU.内存.I/O设备,存储四大部分组成. CPU是整个计算机的核心部件,主要由运算器和控制器组成,它负责整个计算机的程序运行以 ...

  2. 卷积神经网络入门:LeNet5(手写体数字识别)详解

    第一张图包括8层LeNet5卷积神经网络的结构图,以及其中最复杂的一层S2到C3的结构处理示意图. 第二张图及第三张图是用tensorflow重写LeNet5网络及其注释. 这是原始的LeNet5网络 ...

  3. 极限编程核心价值:尊重(Respect)

    原文:https://deviq.com/respect 极限编程核心价值:简单(Simplicity) 极限编程核心价值:沟通(Communication) 极限编程核心价值:反馈(Feedback ...

  4. bower包管理工具

    安装: npm install bower -g (全局安装) 验证: bower --version 指令( 以vue为例 ): 1.  bower info vue              查看 ...

  5. Python返回数组(List)长度的方法

    其实很简单,用len函数: >>> array = [0,1,2,3,4,5]>>> print len(array) 同样,要获取一字符串的长度,也是用这个len ...

  6. Redis系列三:reids常用命令

    全局命令 keys *  查看所有键 dbsize 查看的是当前所在redis数据库的键总数 如果存在大量键,线上禁止使用此指令 exists key 检查键是否存在,存在返回1,不存在返回0 del ...

  7. Tensorflow Object Detection API 安装

    git:https://github.com/tensorflow/models/tree/master/object_detection 中文文档:http://wiki.jikexueyuan.c ...

  8. 【转】android Toast大全(五种情形)建立属于你自己的Toast

    Toast用于向用户显示一些帮助/提示.下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast. 1.默认效果 代码 Toast.makeText(getApplicationCo ...

  9. python利用smtplib和MIMETYPE发送邮件

    # -*- coding:utf- -*- import smtplib from email.mime.text import MIMEText sender = '你的发送邮件' my_pass= ...

  10. 1、pyspider安装

    系统环境: centos6.6.python2.7 经测试,python2.6安装的pyspider会导致webui无法正常访问 参考博文: http://cuiqingcai.com/2443.ht ...